Javascript not working in HTML

Asked

Viewed 185 times

3

Good,

I need a little help here. I tried this code on my computer in an HTML page and it doesn’t work , but in fiddle(https://jsfiddle.net/uxrkgfoz/) It works perfectly. Am I missing anything ?

Thank you

2 answers

5


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

inserir a descrição da imagem aqui

That means all your code will be inside

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

and makes the code only run afterward 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. So make sure HTML has already been read when Javascript starts running.

    ...
    <script>
    // código aqui...
    </script>
</body>

0

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

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

var escopoProjeto = function () {
 //ação do projeto 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.