-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·46 lines (37 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function renderPasswords (passwordElements, passwordLength) {
const randomPasswords = generateRandomPasswords({
passwordLength,
passwordAmount: 4
})
passwordElements.forEach((el, index) => {
el.value = randomPasswords[index]
})
}
function checkLengthNotExceeded (evt) {
const el = evt.target || evt
if(el.type == "number" && el.max && el.min){
let value = parseInt(el.value)
el.value = value // for 000 like input cleanup to 0
let max = parseInt(el.max)
let min = parseInt(el.min)
if ( value > max ) el.value = el.max
if ( value < min ) el.value = el.min
}
}
/* --- EVENT LISTENERS --- */
const passwordElements = document.querySelectorAll(".password")
const generatePasswordsBtn = document.querySelector(".generate-passwords")
const lengthInput = document.querySelector(".length-input")
function assignCopyOnClick (inputElements) {
passwordElements.forEach(input => input.addEventListener("click", () => {
if (input.value){
input.select()
document.execCommand('copy')
}
}))
}
generatePasswordsBtn.addEventListener("click", () => {
renderPasswords(passwordElements, lengthInput.value)
})
assignCopyOnClick(passwordElements)
lengthInput.addEventListener('input', checkLengthNotExceeded);