0

i have following code:

const x = document.createTextNode('Helloooo')
document.body.insertAdjacentText('beforebegin', x) 

and after execution i see something like [object Text].

My question is how do I see the clear text, thus "Helloooo"?

According the documentation:

  • insertAdjacentText (text nodes)
  • insertAdjacentElement (nodes)
  • insertAdjacentHTML (html string)

What does document.createTextNode return? I thought a text node, so I use first method, second doesnt works.

Thx for help.

1
  • This smells like an XY problem. Commented Aug 8, 2022 at 13:39

1 Answer 1

1

createTextNode returns a Node, not a string. The second argument to insertAdjacentText should be a string, not a Node:

const x ='Helloooo';
document.body.insertAdjacentText('beforebegin', x);

If you want x to be a Text Node, you can use x.wholeText to get the text of the Node:

document.createTextNode('Helloooo');
document.body.insertAdjacentText('beforebegin', x.wholeText);
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, is there a way how to convert Text Node to string? Because when i use different function eg, append / appendChild, prepend, thats works!
you can use x.wholeText
Cool, i founded too, but little bit later ;-), Okay, thx!

Your Answer

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.