I am unable to change the contents of my button

Asked

Viewed 35 times

0

I’m trying to change the text of my button when I click on it, but I’m not getting it.

My code:

function altera(argument) {
    document.getElementById('novo').innerHTML="atualizou";	
}
<!DOCTYPE html>
<html>
 <head>
  <title>noono</title>
  <script src="intercao.js"></script>
 </head>
 <body>
 <button id="novo" onclick="altera()">Isso e um teste</button>
 </body>
</html>

1 answer

3


Use the property textContent for that reason:

function altera(argument) {
  document.getElementById('novo').textContent ="atualizou";    
}
 <button id="novo" onclick="altera()">Isso e um teste</button>

The innerHTML is used to include content html with tags, in your case it’s just the text inside a button, so you can use textContent.

The estate innerText has the same "semantic" function, that is to change the text and not content html. Although it is not standard, it is used in Internet Explorer, then if you have problems you can check which to use.

Browser other questions tagged

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