-1

Im trying to add a new text to a existing in the simplest way posible, in my case I can only modify the script that is inside a paragraph element but Im getting this error Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node. How can I make this works in the shortest code possible?

<!-- Many elements above this -->
<p>
  This a part of the text
  <script>
    document.currentScript.parentNode.appendChild(" and this is the new text added");
  </script>
</p>
<!-- Many elements under this -->
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
SilverSurfer
  • 4,281
  • 6
  • 24
  • 50

1 Answers1

4

You should create the text as textNode using createTextNode() method like,

const textNode = document.createTextNode(" and this is the new text added");

and pass the node created as parameter to appendChild like,

document.currentScript.parentNode.appendChild(textNode);

And the modified snippet as sollows,

<!-- Many elements above this -->
<p>
  This a part of the text
  <script>
    const textNode = document.createTextNode(" and this is the new text added");
    document.currentScript.parentNode.appendChild(textNode);
  </script>
</p>
<!-- Many elements under this -->
Maniraj Murugan
  • 8,868
  • 20
  • 67
  • 116