JavaScript: DOM text nodes
-
23-09-2019 - |
Question
Take a look at the snippet below. Does it create a text node for the string "test" in the DOM? Can I select that node with jQuery for MooTools?
<div id="foobar">
test <img />
</div>
Solution
//plain
var node = document.getElementById('foobar').childNodes[0];
//jquery
$("foobar").contents().eq(0);
this will give you a textnode which will include the whitespace around the text too
OTHER TIPS
With jQuery:
Edit:
$('#foobar').get(0).firstChild.data;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow