I'm kind of new to HTML / Javascript, and I'm trying to figure out how to make it so when I click a button, an image loads. This is all I can come up with, but then again I am very bad with this stuff, and I am still learning greatly .
<html>
<div style="visibility:invisible" id="theTrick">
<img id="myImage" src="https://minecraft.net/images/logo.png" alt="Test">
</div>
<button type="button" onclick="myFunction">Click for the Minecraft Logo</button>
<script>
function myFunction() {
document.getElementById="theTrick".style = "visibility:initial"
}
</script>
</html>
I have solved this, and since then learned a LOT more, mastering HTML and CSS, and now learning the works of JavaScript. I do have a question though to add on to this; is there a way to have the image disappear on another click, then reappear on the next click, and so on?
A few things:
visibility: hidden
, not visibility: invisible
. myFunction()
, not myFunction
. document.getElementById("theTrick")
, not document.getElementById = "theTrick"
. visibility: visible
, not visibility: initial
(the initial visibility is hidden
, which you've set at the top). You may find this document on the CSS visibility
property useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.