Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get content in a div without specific child element

<div class="news"> 
    i need this content

   <figure class="summary">
     i dont need this content
    </figure>

 </div>

I want to get "i need this content" in class .news but without "i dont need this content" in class .summary

$("div.news").not("figure.summary").text()

I tried this jquery but still gettiing text in figure tag.

like image 850
Must.Tek Avatar asked Nov 15 '25 00:11

Must.Tek


1 Answers

alert($("div.news").contents().not("figure.summary").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="news"> 
    i need this content

   <figure class="summary">
     i dont need this content
    </figure>

 </div>

You should use contents() to include textNodes:

$("div.news").contents().not("figure.summary").text()

And if you want to get only textNodes, not filtering out by specific class, type, etc...:

$("div.news").contents().not("div.news *").text()

Learn more about .contents() at: http://api.jquery.com/contents/

like image 100
A. Wolff Avatar answered Nov 17 '25 20:11

A. Wolff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!