Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select element based on text

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?

like image 370
s gandhi Avatar asked Jul 02 '10 15:07

s gandhi


People also ask

Is there a CSS selector for elements containing certain text?

The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.

How do I search for text in a div?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do you select an element in CSS?

The CSS id Selector The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.


1 Answers

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

$('a:contains("4")')
like image 96
SLaks Avatar answered Oct 21 '22 17:10

SLaks