Javascript get only div parent node value (no child nodes)
Here is my problem:
<html> <div id="parentdiv"> some parent value <div id="childdiv">some child value</div> </div> </html> parent div → parent div content → child div → child div content → end child div → end parent div
I need to get only the parent div value without the value of the child div. How can I do this in javascript?
+4
4 answers
If you have control over the HTML, just wrap that value with a <p> (as it should be), and then access it like that.
HTML
<html> <div id="parentdiv"> <p>some parent value</p> <div id="childdiv">some child value</div> </div> </html> JQuery
$('#parentdiv p').text(); If there are other <p> elements in the parent element, then use the class.
0