diff --git a/script.js b/script.js index 4bfed27..a4cda53 100644 --- a/script.js +++ b/script.js @@ -1,28 +1,25 @@ ```javascript -function generateQR() { - const inputElement = document.getElementById("text"); - const qrCodeDiv = document.getElementById("qrcode"); +// Get HTML elements +const inputElement = document.getElementById("text"); +const qrCodeDiv = document.getElementById("qrcode"); +const generateButton = document.getElementById("generateButton"); - if (inputElement.value.trim() === "") { +// Validate input and generate QR code on button click +generateButton.addEventListener("click", () => { + if (inputElement.value.trim() === '') { alert("Please enter text or URL"); return; } if (typeof inputElement.value !== "string") { - throw new Error("Input must be a string"); - } - - try { - const qrcode = new QRCode(qrCodeDiv, { - text: inputElement.value, - width: 200, - height: 200 - }); - } catch (error) { - console.error(error); - alert("An error occurred while generating the QR code"); + alert("Input must be a string"); + return; } -} -document.getElementById("generateButton").addEventListener("click", () => generateQR()); + const qrCode = new QRCode(qrCodeDiv, { + text: inputElement.value, + width: 200, + height: 200 + }); +}); ``` \ No newline at end of file