<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")')
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.
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.
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.
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.
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")');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With