views:

34

answers:

2

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?

+4  A: 

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

$('a:contains("4")')
SLaks
A: 

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

Mac
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.
bobince
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.
bobince