How to change text from a header element in JavaScript?

1

What is the best function that changes the text of a header element ( h1 , h2 , h3 , etc) through JavaScript?

I tried this way to change the text:

function mudarTexto(id, novoTexto){
  document.getElementById(id).innerText = novoTexto;
}

But the text remains the same. See the example I created in JSFiddle

    
asked by anonymous 08.10.2014 / 16:25

1 answer

3

Your code is correct and it works. The problem is that jsFiddle encapsulates your JavaScript within an onLoad function and then its function gets out of scope for what's in the html.

Change jsFiddle to noWrap and it works.

Versionworking: link

If you want to use this in a site then you can put the function in the document, wherever you want. Since you are defining a function only, then you do not need to be inside any other function onload or domready .

Example: link

If you want to insert this code into a separate file you can add it to the page with

<script src="oMeuScript.js"></script> 

If you are in the same directory as index.html . If you are not have to show the path from root: src="/ < caminho > /oMeuScript.js"

    
08.10.2014 / 16:44