Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery finding the first P

Tags:

jquery

I have the following:

$(content).filter("p:first").text();

where content =

<div>
<div>
<div>
<p>adsdas</p>
<p>dsa</p>
<p>dsa</p>
<p>dsa</p>
</div>
<p>&nbsp;</p>
<p>asdadsadsads</p>
</div><p><br></p><p><br></p><p>asd</p><p><br></p><p><br></p></div>

What I want to happen is to find the first PARAGRAPH tag, whether it be at the top or down a child level or more of a div. How can I tell jQuery to find the first

tags all the way down until the first match?

Thanks

like image 658
AnApprentice Avatar asked Oct 21 '25 16:10

AnApprentice


1 Answers

You can use .find() to get any level decendant, like this

$(content).find("p:first").text();

If it may be the top level, it's not as clear, you need to do .filter() and .find()...but the results are in order, so still take the first, like this:

$(content).find("p").andSelf().filter("p:first").first().text();
like image 136
Nick Craver Avatar answered Oct 23 '25 07:10

Nick Craver



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!