How to find an element by text using jQuery ?
About contains selector
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
Depending on the element, the matching text can appear directly within or within a descendant of the selected element. The :contains() selector in jQuery is used to select elements containing the specified string. The text must have a matching case to be selected.
Syntax: A string of text to look for. It’s case-sensitive.
- Create an HTML file “index.html” in your local system.
- Follow the basic HTML template and two-paragraph by adding the text inside the <p> tag.
- Select the <p> tag with contains selector and pass the keyword that you want to select as a parameter in contains selector method.
- After you successfully follow the above steps then attach a CSS property to that selected paragraph which contains the keyword that you pass as an argument in the contains method.
Example: In this case, we are looking for the element that contains the word “Gfg”. We change the color of the particular paragraph that contains the word “Gfg” after selecting the element.