-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (43 loc) · 1.31 KB
/
script.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
47
48
const qrText = document.getElementById('qr-text');
const sizes = document.getElementById('sizes');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn');
const qrContainer = document.querySelector('.qr-body');
let size = sizes.value;
generateBtn.addEventListener('click',(e)=>{
e.preventDefault();
isEmptyInput();
});
sizes.addEventListener('change',(e)=>{
size = e.target.value;
isEmptyInput();
});
downloadBtn.addEventListener('click', ()=>{
let img = document.querySelector('.qr-body img');
if(img !== null){
let imgAtrr = img.getAttribute('src');
downloadBtn.setAttribute("href", imgAtrr);
}
else{
downloadBtn.setAttribute("href", `${document.querySelector('canvas').toDataURL()}`);
}
});
function isEmptyInput(){
// if(qrText.value.length > 0){
// generateQRCode();
// }
// else{
// alert("Enter the text or URL to generate your QR code");
// }
qrText.value.length > 0 ? generateQRCode() : alert("Enter the text or URL to generate your QR code");;
}
function generateQRCode(){
qrContainer.innerHTML = "";
new QRCode(qrContainer, {
text:qrText.value,
height:size,
width:size,
colorLight:"#fff",
colorDark:"#000",
});
}