After following an online tutorial on creating a QR code generator, the code works fine and I am able to display the image. However, I'm facing an issue where the image disappears from the screen once the button is released.
Here is the code snippet:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./style2.css">
<style>
.container.active .qr-code {
display: block;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>QR Code Generator</h1>
<p>Type a URL or text to generate a QR Code</p>
</div>
<div class="input-form">
<input type="text" class="qr-input" placeholder="Enter URL or text">
<button class="generate-btn">Generate QR Code</button>
</div>
<div class="qr-code">
<img class="qr-image" alt="">
</div>
</div>
<script>
var container = document.querySelector(".container");
var generateBtn = document.querySelector(".generate-btn");
var qrInput = document.querySelector(".qr-input");
var qrImg = document.querySelector(".qr-image");
generateBtn.addEventListener("click", function(event) {
if (qrInput.value.length > 0) {
event.preventDefault(); // Prevent form submission
container.classList.add("active");
var qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + encodeURIComponent(qrInput.value);
qrImg.src = qrUrl;
}
});
</script>
</body>
</html>
CSS:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: 'Poppins';
}
body {
width: 100%;
height: 100vh;
background-color: #ff676d;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background-color: #fff;
width: 400px;
border-radius: 7px;
padding: 20px;
height: 400px;
transition: .1s;
}
.header h1{
font-size: 23px;
font-weight: 500;
margin-bottom: 5px;
}
.header p {
font-size: 16px;
margin-bottom: 10px;
}
input, button {
width: 100%;
height: 50px;
outline: none;
border-radius: 5px;
}
button {
border: none;
background-color: #1d68d8;
font-size: 15px;
columns: #fff;
cursor: pointer;
}
input {
border: 1px solid #8b8a8a;
padding-left: 10px;
margin-bottom: 15px;
font-size: 15px;
}
.qr-code {
padding: 25px 0;
border: 1px solid #ccc;
margin-top: 10px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
opacity: 0;
pointer-events: none;
transition: .5s;
}
.container:active {
height: 490px;
}
.container:active .qr-code {
opacity: 1;
pointer-events: auto;
}
I'm relatively new to this and have tried modifying the display property within the CSS for the `qr-code` section. But upon further reading, it seems like that might not be the correct approach.
Perhaps I'm overlooking something in the HTML structure.