Skip to content

Commit ef0b654

Browse files
committed
add copy password functionality and improve button layout
Signed-off-by: TBS96 <9tbs6@proton.me>
1 parent a72c432 commit ef0b654

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
<h1>Password Strength Checker</h1>
1414

15-
<button class="passGenBtn">Generate Password</button>
15+
<div class="btns">
16+
<button class="passGenBtn">Generate Password</button>
17+
<button id="copyBtn"></button>
18+
</div>
1619

1720
<div id="password"></div>
1821

js/script.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const passGenBtn = document.querySelector('.passGenBtn');
2+
const passwordShow = document.getElementById('password');
3+
const copyBtn = document.getElementById('copyBtn');
24

35
const generatePassword = passGenBtn.addEventListener('click', () => {
46
const lengthNum = 12;
@@ -12,7 +14,6 @@ const generatePassword = passGenBtn.addEventListener('click', () => {
1214
console.log(randomIndex, password, i);
1315
}
1416

15-
const passwordShow = document.getElementById('password');
1617
passwordShow.textContent = password;
1718

1819
checkStrength(password);
@@ -47,6 +48,30 @@ const changeBgColor = (strength) => {
4748
document.body.style.backgroundColor = bgColors[strength - 1] || '#ffd4d4';
4849
};
4950

51+
copyBtn.innerText = '📋';
52+
53+
copyBtn.addEventListener('click', async () => {
54+
if (!passwordShow.textContent) {
55+
alert('No password to copy!');
56+
return;
57+
}
58+
59+
try {
60+
await navigator.clipboard.writeText(passwordShow.textContent);
61+
passwordShow.style.backgroundColor = 'lightgreen';
62+
setTimeout(() => {
63+
passwordShow.style.backgroundColor = 'transparent';
64+
}, 2000);
65+
copyBtn.innerText = '✔️';
66+
67+
setTimeout(() => {
68+
copyBtn.innerText = '📋';
69+
}, 2000);
70+
}
71+
catch (err) {
72+
console.error(`Failed to copy: ${err}`);
73+
}
74+
})
5075

5176

5277

styles/style.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ body {
1010
border: 5px solid green;
1111
background-color: cornsilk;
1212
}
13+
.btns {
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
gap: 10px;
18+
}
1319

1420
.passGenBtn {
1521
padding: 10px 20px;
1622
background-color: green;
1723
color: white;
1824
font-size: 18px;
1925
border: none;
26+
border-radius: 5px;
2027
cursor: pointer;
2128
transition: 0.5s ease;
2229
}
@@ -40,4 +47,23 @@ body {
4047
margin-top: 20px;
4148
font-size: 1.2em;
4249
font-weight: bold;
50+
}
51+
52+
#copyBtn {
53+
padding: 10px 20px;
54+
background-color: rgb(40, 40, 168);
55+
font-size: 18px;
56+
border: none;
57+
border-radius: 5px;
58+
cursor: pointer;
59+
transition: 0.5s ease;
60+
}
61+
62+
#copyBtn:hover {
63+
background-color: rgba(40, 40, 168, 0.87);
64+
}
65+
66+
#copyBtn:active {
67+
background-color: rgb(94, 224, 94);
68+
transform: translateX(12px);
4369
}

0 commit comments

Comments
 (0)