+ 5

how to remove an image on button click in html???

25th Jul 2017, 9:50 AM
AKSHAY
AKSHAY - avatar
3 odpowiedzi
+ 3
As suggested by @ValentinHacker, with JS, on click event of the targeted button, get reference of your <img> element, and remove it from the DOM... or set it 'display:none' or 'visibility:hidden' css properties/values... or only with Css, design a button with the <label> related to an invisible radio previous sibling of your image, and define one of the quoted css property/value on radio element checked (or unchecked, and made it visible in the other state): <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> #img-cmd { display:none; } #img-cmd:checked+img { display:none; } button>label { display:block; } img { width:80vmin; margin:1em; } body, button>label { text-align:center; font-size:5vmin; } </style> </head> <body> <input type="radio" id="img-cmd"> <img src="http://www.newsnish.com/wp-content/uploads/2015/01/happy-computer.jpg"> <br> <button><label for="img-cmd">remove image</label></button> </body> </html> You can do a 'toggle' button as well, by using a checkbox type <input> instead a radio ;) https://code.sololearn.com/WA71MLneu6VN/#html
25th Jul 2017, 11:12 AM
visph
visph - avatar
+ 8
<button onclick=elm=document.getElementsByTagName("img")[0];elm.parentNode.removeChild(elm)>Image Remover</button>
25th Jul 2017, 9:58 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 5
thanks u both☺☺☺
25th Jul 2017, 11:25 AM
AKSHAY
AKSHAY - avatar