Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy feature in password generator project #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Password Generator/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,18 @@ input:checked + .slider:before {

.slider.round:before{
border-radius: 50%;
}
}

.copy_password{
height:fit-content;
width:fit-content;
display: flex;
margin-top: 30px;
margin-left: 12px;
margin-right:29px;
}
.copy_password:hover{
background-color: #10B981;
border:none;
}

4 changes: 4 additions & 0 deletions Password Generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="index.css" />
<title>Password Generator</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div class="container">
Expand All @@ -16,15 +17,18 @@ <h2 id="headline2">random password</h2>
<div class="password-container">
<button id="PassContainer">
<p class="Password1"></p>
<button class="copy copy_password"><i class="fa-regular fa-copy"></i></button>
<button id="PassContainer">
<p class="Password2"></p>
<button class=" copy copy_password"><i class="fa-regular fa-copy"></i></button>
</div>
<label class="switch" for="modeChanger">
<input type="checkbox" id="modeChanger">
<span class="slider round"></span>
</label>

</div>

</body>
<script src="index.js"></script>
</html>
43 changes: 43 additions & 0 deletions Password Generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,46 @@ const displayPassword = () => {
};

document.getElementById("btn").addEventListener("click", displayPassword);



function copyToClipboard() {
let passwordField = document.getElementById("Password1");

if (passwordField.value) {
passwordField.select(); // Select the text
document.execCommand("copy"); // Copy the text
let copyMsg = document.getElementsByClassName("copy_password");
copyMsg.style.display = "inline"; // Show "Copied!" message

setTimeout(() => {
copyMsg.style.display = "none"; // Hide message after 2 seconds
}, 2000);
}
}


// document.querySelectorAll.(".copy_password").addEventListener("click", function () {
// // Find the text you want to copy (modify selector accordingly)
// const textToCopy = document.querySelector(".Password1").innerText || document.querySelector(".Password2").innerText;
// // Copy the text to clipboard
// navigator.clipboard.writeText(textToCopy).then(() => {
// alert("Copied to clipboard!"); // Optional: Show confirmation
// }).catch(err => {
// console.error("Failed to copy:", err);
// });
// });

document.querySelectorAll(".copy_password").forEach(button => {
button.addEventListener("click", function () {
// Find the closest password element
const textToCopy = this.previousElementSibling.innerText || this.previousElementSibling.value;

// Copy to clipboard
navigator.clipboard.writeText(textToCopy).then(() => {
alert("Copied: " + textToCopy); // Optional feedback
}).catch(err => {
console.error("Failed to copy:", err);
});
});
});