I am trying to run this code:
<style type="text/css">
#boton{
width: 50px;
height: 30px;
display: block;
margin:30px;
}
img{
width: 300px;
height: 200px;
margin:30px;
}
</style>
<script type="text/javascript">
var boton = document.getElementById('boton');
boton.onclick = function(){
var imagen = document.getElementById('foto');
imagen.style.display ='none';
}
</script>
</head>
<body>
<button id="boton">Click</button>
<img src="header.jpg" id="foto">
</body>
The problem is that it doesn't load the event onclick
in any way. I have also tried to create the function before and call it when the event happens, but it doesn't work. The only way that works for me is by entering the code inline, and I don't want to do it that way. The idea is to make all the javascript code in a separate file.
It is possible that it could be because the document has not finished parsing and loading yet.
As a recommendation, some important points:
Be aware of the HTML parsing and loading process
Every document goes through a parsing stage and is finally loaded and becomes available. The DOM (Object Document Model) is an interface that allows you to interact with the document. This interface is in charge of interacting with the HTML, storing each tag in a Node within a tree, which is nothing more than the representation of the tag in an interface.
What does this have to do with your question? That when you interact with the document when it hasn't been fully loaded yet, you will most likely get a null reference because it hasn't been parsed yet and isn't available in the DOM tree .
A good practice is to select the elements when the document has been loaded . In pure javascript it is done by capturing the event
DOMContentLoaded
:In jquery it is done by custom event
ready
:This ensures that the nodes are available in the DOM tree.
Don't put scripts in the header
Unless you mark these scripts with the
async
or attributedefer
, do not put them in the header because document parsing will not proceed until the scripts have been resolved. Instead put the scripts before the closing body:Keep these points in mind when interacting with the elements.
Try making the function
ocultar()
and assigning theonclick
as an attribute of the button.If not, try JQuery:
According to the html flow : first the is executed
<script>
and then the<body>
, I recommend you place the<script>
after the<body>
:The problem you are running into is that the button is not yet loaded in the DOM when the javascript is executed. In the jsFiddle that they provided in the comment, this does not happen, since jsFiddle already takes care of loading the javascript in the corresponding event.
There are many ways to get your code to run when the DOM has finished loading.
Put the code at the end of the body.
We reverse the order and put the script tag at the end of the body tag:
We use the 'DOMContentLoaded' event of the object
document
:We use the 'load' event of the object
window
:OR
Let's take the 'onreadystatechange' event of the object
document
:We use the 'onload' event of the object
window
:Why are there so many events and so many possibilities? Isn't that a little more confusing?
The main reason is due to a compatibility issue. For example
DOMContentLoaded
, it is the event that is most widespread, but it does not work in IE8 and earlier.