1

Is it possible to select the content/text node of a tag using css selectors?

For example I have some content such <div><p class="x">Hello world</p></div>. How can I get/select the text node ("hello world") using CSS?

I know that I could get the p element using .x class selector and then use innerHTML with javascript but I would like to know if it's possible to get the exactly text node using CSS and just set the node data (which is basically text as the node is a text node). Is it possible?

mike
  • 533
  • 1
  • 7
  • 24

1 Answers1

0

No, it is not possible.

However, if you're trying to figure out how to select the text node with querySelector, do what @j08691 suggested:

document.querySelector('.x').textContent

That or

document.querySelector('.x').firstChild.nodeValue

should work.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jayly
  • 156
  • 6