Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find deepest child, :contains(string)

Tags:

jquery

When I use the :contains selector, it returns all elements (including parents/ancestors) matching the selector. How can I just return the child-most/deepest element that matches?

Example:

<div>
    <div>some text</div>
    <div>more text</div>
    <div>
        even more text
        <div>Copyright 2013</div> <!-- I only want this div -->
    </div>
</div>

.

console.log($(':contains(Copyright)'));

JSFiddle: http://jsfiddle.net/rQ7qP/

I don't know anything about the containing page, i.e. It could be in any element type, or with any class/id attribute. Nothing can be assumed.

like image 733
Drahcir Avatar asked Oct 28 '13 15:10

Drahcir


1 Answers

Try

console.log($(':contains(Copyright):not(:has(*))'));

Demo: Fiddle

like image 70
Arun P Johny Avatar answered Sep 22 '22 17:09

Arun P Johny