Reputation: 371
In an HTML I created a "div" with an unique id. Within this "div" I have more "div"s, in which each contains a string. For example:
<div id="name">
<div>A</div>
<div>B</div>
</div>
What I want to do is to find the index of a "div" that contains a string, say "A", within the "div" with id="name".
I think selector ":contains" should be used in combination with .index() but I can't figure out the right syntax. This is what I've tried:
var index = $("#name"):contains(A).index("div");
Upvotes: 3
Views: 3919
Reputation: 55678
I think you want
var index = $('#name div:contains("A")').index();
That will find the element matching the selector div:contains('A')
within the element with id = name.
As you've probably worked out by now, selectors prefixed with :
are meant to be used within the selector string. Many have corresponding jQuery functions that can be chained instead (e.g. :eq()
and .eq
), but :contains()
does not (the function .contains()
tests for DOM elements within other elements, rather than looking at text).
Upvotes: 5
Reputation: 18557
try this
var index = $("#name>div:contains(A)").eq(0).index();
Upvotes: 3