Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all links with specific inner HTML value in jQuery

Tags:

<div>     <a>        Text1        <img alt="" stc="" />     </a>     <a>        Text2     </a>  </div> 

I want to select all anchor elements that have text=text2. I'm looking for something like this:

$('a[text=Text2]') 

Edit: Why this is not working? For some some reason, it needs to be in this format:

$('div').find('a').find(':contains("Text2")') 
like image 898
Fitzchak Yitzchaki Avatar asked Feb 11 '10 02:02

Fitzchak Yitzchaki


People also ask

How to get inner HTML in jQuery?

jQuery html() MethodThe html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element. When this method is used to set content, it overwrites the content of ALL matched elements.

Can you write a jQuery code to select all links inside the paragraph?

To select all links inside paragraph element, we use parent descendant selector. This selector is used to selects every element that are descendant to a specific (parent) element.

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

How do I get the innerHTML value?

Firstly, to get the innerHTML value of any tag, you either need that tag to have its 'id' property or 'name' property set. Then you can respectively use the 'document. getElementById(yourTagIdValue). innerHTML' or 'document.


1 Answers

You ask why this doesn't work:

$('div').find('a').find(':contains("Text2")') 

The reason is, .find() will search children elements, you want .filter() (because you already selected the a - or you add the :contains to the a find:

$('div').find('a').filter(':contains("Text2")'); $('div').find('a:contains("Text2")'); 
like image 150
gnarf Avatar answered Sep 22 '22 22:09

gnarf