-9

say I have a <p> tag like this

<p>some content</p>

Is there a way to add more to this, and make it for example

<p>some content on the site</p>

Just to clarify I don't want to replace it. I know about both the innerHTML method and .update() but these replace the text not add to it.

mmoomocow
  • 1,173
  • 7
  • 27
  • 5
    Have you tried googling first? Check this post: https://stackoverflow.com/questions/121817/how-do-i-replace-text-inside-a-div-element/121824 – Beri Mar 10 '20 at 10:29
  • 3
    Does this answer your question? [How do I replace text inside a div element?](https://stackoverflow.com/questions/121817/how-do-i-replace-text-inside-a-div-element) – Alex Mar 10 '20 at 10:30
  • @Alex I saw that, but I want to add to the text, not replace it – mmoomocow Mar 12 '20 at 07:16

2 Answers2

2

You can simply fetch the innerText and update with new value:

document.getElementById("myContent").innerText = document.getElementById("myContent").innerText + " is now changed";
<p id="myContent">some content</p>
Muhammad Zeeshan
  • 4,608
  • 4
  • 15
  • 41
0

it must be somehow you can find that <p>, based on how your document is:

  • Using the "id" attribute
  • Using getElementBy... (tagName, class)...

If the

content is simply a text, just use textContent If it is more complicated, consider using innerHTML instead.