When a button is clicked, I'm displaying an image (in a JS function) however the function prints the image every time the button is clicked. I want the function to run only once so that the image can only be displayed once. What do I need to add to my code to do this?
<button class="colour-btn colour-btn--3" onClick="showImage3()"></button>
function showImage3() {
var img3 = document.createElement('img')
img3.setAttribute("src", "")
img3.setAttribute("width", "700")
img3.setAttribute("height", "400")
document.body.appendChild(img3)
}
You can redefine the function once it is run the first time so it only runs once.
function showImage3() {
showImage3 = function() {};
var img3 = document.createElement('img')
img3.setAttribute("src", "")
img3.setAttribute("width", "700")
img3.setAttribute("height", "400")
document.body.appendChild(img3)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments