Patrik
Patrik

Reputation: 1327

Get text with html tags from main DIV by jquery?

How to get text with html tags, but only from main DIV? Not form other DIVS.

Here is example, but there is some problem, becouse text is without html tag <br />

jsFiddle DEMO

HTML

<div id='parent'>
    this text is <br />for parent

   <div id='child-parent'> 
     this text if for child-parent  
       <div id='child'>
        and this text is for child.   
       </div>   
   </div>
</div>

jQuery

alert($('#parent').clone().children().remove().end().html());

Upvotes: 0

Views: 156

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

Assuming child-parent and child will always have an id

var clone = $('#parent').clone();
clone.children().filter(function () {
    return $(this).is('[id]');
}).remove();

console.log(clone.html());

Demo ---> http://jsfiddle.net/F6AeM/5/

Upvotes: 1

Smeegs
Smeegs

Reputation: 9224

I updated your fiddle and used a different tactic to remove the last element

http://jsfiddle.net/F6AeM/1/

var parent = $('#parent').clone();
$(parent).find('#child-parent').remove();
alert($(parent).html());

Not sure why this works, but yours doesn't, but here you go.

Upvotes: 1

Related Questions