6

What seems to be the problem? The code doesn't work, the text does not change.

function translate() {
    document.getElementById("tex").innerHTML = "BLABLA";
}
<h1 align="center"><font size="100">What Is BLA: </font></h1>
<p id ="tex"><font size="10">BLA</font></p>
<button onclick="translate()">Translate</button>

3
  • move button inside the body Commented Dec 13, 2016 at 21:14
  • The button is inside O_o Commented Dec 13, 2016 at 21:16
  • if you change the function name it works fine. plnkr.co/edit/f0x2eHG3aKfrziLtDHxI?p=preview Commented Dec 13, 2016 at 21:18

2 Answers 2

13

The problem is in some browsers, like Chrome, DOM elements have a translate property (MDN does not list Chrome as supporting this feature, but it does have the property). In the context of JavaScript event attributes, those properties shadow any globals of the same name.

If you check your developer console, you should see a message saying that translate is not a function because of this.

Uncaught TypeError: translate is not a function

If you change the name of the function, you will avoid this issue:

function myTranslate() {
    document.getElementById("tex").innerHTML = "BLABLA";
}
<h1 align="center"><font size="100">What Is BLA: </font></h1>
<p id ="tex"><font size="10">BLA</font></p>
<button onclick="myTranslate()">Translate</button>

Sign up to request clarification or add additional context in comments.

1 Comment

This is the problem at hand, yes! I was trying to dig up some documentation to verify it so I didn't write up an answer myself.
0

Maybe also to note is that you are trying to set the innerHTML of the p element with the id tex. But the element has a child element in it. You can set the innerHTML property of that item.

document.getElementById("tex").childNodes[0].innerHTML = "BLABLA";

2 Comments

Or better yet, don't use the <font> tag and use CSS instead!
Agreed, don't use the font tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.