0
Hello, I have the following code that is working but I want to improve it. Any suggestions?
click event add
[1, 2, 3].forEach(num => {
document.getElementById(`btn-lupa${num}`).addEventListener("click", () => {
esquemaLupa(num);
});
});
hide function
div
function esquemaLupa(num) {
if (num == 1) {
document.getElementById(`feed-lupa-${num}`).style.display = "block";
document.getElementById(`feed-lupa-2`).style.display = "none";
document.getElementById(`feed-lupa-3`).style.display = "none";
}
if (num == 2) {
document.getElementById(`feed-lupa-${num}`).style.display = "block";
document.getElementById(`feed-lupa-1`).style.display = "none";
document.getElementById(`feed-lupa-3`).style.display = "none";
}
if (num == 3) {
document.getElementById(`feed-lupa-${num}`).style.display = "block";
document.getElementById(`feed-lupa-2`).style.display = "none";
document.getElementById(`feed-lupa-1`).style.display = "none";
}
}
Html
<button id="btn-lupa1">1</button>
<button id="btn-lupa2">2</button>
<button id="btn-lupa3">3</button>
<div id="feed-lupa-1">div1</div>
<div id="feed-lupa-2">div2</div>
<div id="feed-lupa-3">div3</div>
blocks the HTML code?
– novic
Guy use a class . active and apply only on the element clicked and remove from brothers. Put
.style.display = "none/block";
is not a good choice...– hugocsl
would be like this: <button id="btn-lupa1">1</button> <button id="btn-lupa2">2</button> <button id="btn-lupa3">3</button> <div id="feed-lupa-1">div1</div> <div id="feed-lupa-2">div2</div> <div id="-feed-lupa-3">div3</div>
– DMiaguy