georgiana_e
georgiana_e

Reputation: 1869

modify a text javascript

How can I set an ID for a text which I want to modify later in javascript.

Example:
<hr>
<b>Text:</b>
This is my text to modify.

<hr>
<b>Next Text:</b>
AAA
....

I want to modify the text "This is my text to modify." in javascript. I need something like the code below, but how can I set an id for the text I need to modify?

document.getElementById('?').innerHTML="modified text";

Thanks for your help

Upvotes: 0

Views: 965

Answers (5)

chris97ong
chris97ong

Reputation: 7070

HTML:

Example:
<hr>
<b>Text:</b>
<a id="myTxt">This is my text to modify.</a>

<hr>
<b>Next Text:</b>
AAA
....

And the JS:

document.getElementById("myTxt").innerHTML = "Modified!";

Upvotes: 0

chandu
chandu

Reputation: 2276

As per your code, Do something like this

Example:
<hr>
<b>Text:</b>
<span id="id1">This is my text to modify.</span>

<hr>
<b>Next Text:</b>
AAA
....

And JS like this

document.getElementById('id1').innerHTML="modified text";

Upvotes: 0

sylwester
sylwester

Reputation: 16508

You have to wrap text in html tag ie:

 <span id="textToModify">Some text</span>

and after that you can

document.getElementById('textToModify').innerHTML="modified text";

Upvotes: 1

Albzi
Albzi

Reputation: 15619

Do something like this:

<span id="modify">This is the text to modify</span>

Then in JavaScript:

document.getElementById('modify').innerHTML="New text ";

NOTE: Instead of <span>, you could also use <p>, <h1> - <h6> or any other element.

Upvotes: 2

Pete TNT
Pete TNT

Reputation: 8403

Wrap the "This is my text to modify." into a element and modify that

 <p id="text-to-modify">This is my text to modify</p>
 document.getElementById('text-to-modify').innerHTML="modified text";

Upvotes: 0

Related Questions