10

I want to select an element based on their innertext in JQuery. Below is my Html

<html> <body> <a href="fddf">4</a> <a href="fddf">4</a> <a href="fddf">4</a> </body> </html> 

I want to select all the <a> tags which has text of "4". Any idea how do I do it?

2
  • You can add HTML or source code to your question by indenting it with four spaces. (or by clicking the Format Code (1010) button in the toolbar) Commented Jul 2, 2010 at 15:57
  • possible duplicate of stackoverflow.com/questions/1430290/jquery-select-based-on-text Commented Apr 19, 2012 at 5:58

2 Answers 2

16

You're looking for the :contains selector, like this:

$('a:contains("4")') 
Sign up to request clarification or add additional context in comments.

Comments

-1

I think the correct way is $("a[outerText='4']"). Contains returns 4,44,444 etc. .

2 Comments

No, that selects all a elements with an attribute outerText valued 4: eg.: <a outerText="4">. Since there is no HTML attribute called outerText that is probably not what you want. There is a DOM property with the same name in IE (and WebKit and Opera which copied the extension), and jQuery's selector engine will incorrectly use it when available because IE gets confused about the difference between an attribute and a property.
Even when the outerText property is available, the above code will fail on all modern browsers, because the $() will get optimised out to a call to the browser's native querySelectorAll() method. This method interprets the selector correctly and thus will not allow you to use outerText in an attribute selector to refer to the property of the same name. So this hack will only work on IE6-7.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.