0

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

asked May 15, 2014 at 8:49

5 Answers 5

2

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.

answered May 15, 2014 at 8:51
Sign up to request clarification or add additional context in comments.

Comments

1

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";
answered May 15, 2014 at 8:52

Comments

0

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";
answered May 15, 2014 at 8:51

Comments

0

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";
answered May 15, 2014 at 8:54

Comments

0

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!";
answered May 15, 2014 at 8:55

3 Comments

Why did you use an a tag?
That is what I usually do.
<a> is meant for links.. you could have added either <p> or <span>

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.