user2133404
user2133404

Reputation: 1857

get anchor with a specific text inside li using Jquery

I have the following html code

<ul id="selector">
  <li> <a> first </a> </li>
  <li> <a> Second </a> </li>
</ul>

I want to get the anchor with specific text and trigger a click event on it.

$("#selector li").find('a').contains('first').click()

Error :

$(...).find(...).contains is not a function

Upvotes: 0

Views: 4186

Answers (2)

Milind Anantwar
Milind Anantwar

Reputation: 82241

You are missing the double quote " at beginning of li selector. you also need to use selector :contains instead of .contains() because:

.contains() Check to see if a DOM element is a descendant of another DOM element.

and

:contains Select all elements that contain the specified text.

 $("#selector li").find('a:contains(first)').click()

Upvotes: 2

ozil
ozil

Reputation: 7117

<ul id="selector">
  <li> <a> first </a> </li>
  <li> <a> Second </a> </li>
</ul>  

$("#selector li").find('a:contains(first)').click(function(){
alert('');
});

demo

Upvotes: 2

Related Questions