jQuery Topics
- jQuery Introduction
- jQuery Callbacks
- jQuery deferred
- jQuery selectors
- *
- :animated
- [name|=”value”]
- [name*=”value”]
- [name~=”value”]
- [name$=”value”]
- [name=”value”]
- [name!=”value”]
- [name^=”value”]
- :button
- :checkbox
- :checked
- Child Selector
- .class
- :contains()
- Descendant Selector
- :disabled
- Element
- :empty
- :enabled
- :eq()
- :even
- :file
- :first-child
- :first-of-type
- :first
- :focus
- :gt()
- Has Attribute
- :has()
- :header
- :hidden
- #id
- :image
- :input
- :lang()
- :last-child
- :last-of-type
- :last
- :lt()
- [name=”value”][name2=”value2″]
- (“selector1, selector2, selectorN”)
- (“prev + next”)
- (“prev ~ siblings”)
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :odd
- :only-child
- :only-of-type
- :parent
- :password
- :radio
- :reset
- :root
- :selected
- :submit
- :target
- :text
- :visible
- jQuery Ajax Events
- jQuery Ajax Methods
- jQuery Keyboard Events
- jQuery Keyboard Methods
- jQuery Form Events
- jQuery Form Methods
- jQuery Mouse Events
- jQuery Mouse Methods
- jQuery Event Properties
- jQuery Event Methods
- jQuery HTML
- jQuery CSS
- jQuery Fading
- jQuery Traversing
- jQuery Utilities
- jQuery Properties
jQuery :contains() Selector
Photo Credit to CodeToFun
🙋 Introduction
In web development, jQuery offers a plethora of powerful tools to manipulate HTML elements efficiently. One such tool is the :contains()
selector, which enables you to select elements based on their textual content. Understanding and mastering this selector can significantly enhance your ability to interact with and manipulate HTML elements containing specific text.
This guide will explore the usage of the jQuery :contains()
selector with comprehensive examples to illustrate its utility.
🧠 Understanding :contains() Selector
The :contains()
selector allows you to target elements containing specific text within their content. This selector is particularly useful when you need to select elements dynamically based on their textual content.
💡 Syntax
The syntax for the :contains()
selector is straightforward:
$("selector:contains('text')")
📝 Example
Selecting Elements Containing Text:
Suppose you have a list of elements and you want to select those containing a specific text. You can achieve this using the
:contains()
selector as follows:index.htmlCopied<ul> <li>Apple</li> <li>Banana</li> <li>Orange</li> </ul>
example.jsCopied$("li:contains('Banana')").css("color", "green");
This will change the color of the list item containing the text Banana to green.
Filtering Table Rows Based on Content:
Consider a scenario where you have a table and you want to filter rows based on a specific text within one of the columns. You can use the
:contains()
selector for this purpose:index.htmlCopied<table> <tr> <td>John</td> <td>Doe</td> </tr> <tr> <td>Jane</td> <td>Smith</td> </tr> </table>
example.jsCopied$("td:contains('Doe')").parent().css("background-color", "lightblue");
This will highlight the entire row where the last name is Doe with a light blue background.
Dynamically Updating Content:
You can also dynamically update the content of elements based on specific text using jQuery and the
:contains()
selector. For example:index.htmlCopied<div class="message">Hello, World!</div> <div class="message">Goodbye, World!</div>
example.jsCopied$(".message:contains('Hello')").text("Greetings!");
This will change the text of the div containing Hello to Greetings!.
Case Sensitivity:
Keep in mind that the
:contains()
selector is case-sensitive. Ensure that the text you provide matches the case of the content you're targeting.
🎉 Conclusion
The jQuery :contains()
selector is a versatile tool for selecting and manipulating HTML elements based on their textual content. Whether you need to select elements, filter table rows, dynamically update content, or perform other text-based operations, this selector offers a convenient solution.
By mastering its usage, you can efficiently interact with and manipulate elements containing specific text, thereby enhancing the interactivity and functionality of your web pages.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery :contains() Selector), please comment here. I will help you immediately.