JavaScript without working in HTML

3

Good,

I need a little help which is as follows. I tried this code on my computer on an HTML page and it does not work, but on the fiddle ( link ) works perfectly. Am I missing something?

Thank you

    
asked by anonymous 14.06.2016 / 00:10

2 answers

5

Notice that jsFiddle has an option that inserts your code into a onLoad function.

That means all of your code will stay inside

window.onload = function(){
    // aqui
}

and causes the code to only be after the HTML has been read. What happens to you now is that JavaScript runs and does not find the HTML you are looking for.

Solution:

or you do with window.onload = ... or you can put your code at the bottom of the page before the end of the body. This ensures that HTML has already been read when JavaScript starts running.

    ...
    <script>
    // código aqui...
    </script>
</body>
    
14.06.2016 / 00:16
0

Another suggestion is to do this with a list event, for example:

window.onload = function() { 
    var el = document.getElementById("elementoAlvo"); 
        el.addEventListener("click", escopoProjeto, false); 
}

var escopoProjeto = function () {
 //ação do projeto 
}
    
27.06.2016 / 23:31