Jquery Filtering
jQuery offers a wide range of filtering methods to help you easily and efficiently manipulate and traverse the elements of an HTML document. Here are some of the most commonly used filtering methods:
- :first – Selects the first element in the set of matched elements.
- :last – Selects the last element in the set of matched elements.
- :even – Selects even-indexed elements in the set of matched elements.
- :odd – Selects odd-indexed elements in the set of matched elements.
- :eq(n) – Selects the element at index n within the set of matched elements.
- :gt(n) – Selects elements with an index greater than n within the set of matched elements.
- :lt(n) – Selects elements with an index less than n within the set of matched elements.
- :not(selector) – Selects all elements that do not match the given selector.
- :has(selector) – Selects elements that have at least one descendant that matches the given selector.
- :contains(text) – Selects elements that contain the specified text.
Here is an example of how to use some of these filtering methods:
// Select all even-indexed paragraphs on the page and hide them $("p:even").hide(); // Select all input elements except for the ones with the "disabled" attribute $("input:not([disabled])").css("background-color", "yellow"); // Select all elements that contain the text "Lorem" and change their font size $(":contains('Lorem')").css("font-size", "18px");
These are just a few of the many filtering methods that jQuery offers. You can find a full list of filtering methods in the jQuery documentation.