some parent value
s...">

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
source share
4 answers

try the following:

 alert($('#parentdiv').clone().children().remove().end().text()); 
+2
source

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
source

It works:

 var $parent = $('#parentdiv').clone(); $parent.find('#childdiv').remove(); var parentvalue = $parent.text(); 

Try this JsFiddle

0
source

just wrap it inside the span tag and get inneHTML of that tag

0
source

All Articles