Change Javascript text

Asked

Viewed 19,011 times

2

I want to change the text of the text tag with Javascript.

<text id =txtrect x="5" y="35" font-family="Verdana" font-size="11" fill="white" >
  Rect
</text>

I tried this but it doesn’t change

document.getElementById(txtrect).innerHTML=teste;
  • 1

    Try to replace ìd =txtrect for id="txtrect" and document.getElementById(txtrect).innerHTML=teste; for document.getElementById("txtrect").innerHTML = "teste"; And study the javascript syntax. In programming, each comma can change the result. These missing quotes denote that you are starting. So from now on, pay more attention when typing code. Not to mention that attributes x and y should not exist.

2 answers

4


Change to:

<text id="txtrect" x="5" y="35" font-family="Verdana" font-size="11" fill="white" >
  Rect
</text>

and

document.getElementById("txtrect").innerHTML="teste";

That should solve your problem.

Update

I advise you to take a look at this tutorial: http://www.w3schools.com/js/js_htmldom_methods.asp Will solve your problems with javascript.

-1

Just do it like this:

document.querySelector("#txtrect").innerHTML ="Teste";
<text id =txtrect x="5" y="35" font-family="Verdana" font-size="11" fill="white" >
  Rect
</text>

Browser other questions tagged

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