What is the has() method in jQuery? Explain it with the help of an example.
has() method in jQuery
263
29-Jul-2021
Aryan Kumar
14-Jun-2023The has() method in jQuery is used to select all elements that have one or more elements inside of them that match the specified selector. The selector can be a string or a jQuery object.
The following code selects all <div> elements that have a <span> element inside of them:
Code snippet
The following code selects all <p>, <h3>, and <div> elements that have a <span> element inside of them:
Code snippet
The following code selects a <span> element that has a hyperlink:
Code snippet
The has() method can also be used to select elements that have multiple elements inside of them that match the specified selector. For example, the following code selects all <div> elements that have both a <span> element and an <a> element inside of them:
Code snippet
The has() method is a powerful tool that can be used to select elements based on their descendants. It can be used to select elements that have specific content, elements that have a certain structure, or elements that are related to other elements.
Here are some additional examples of how the has() method can be used:
Code snippet
Code snippet
Code snippet
The has() method is a versatile tool that can be used to select elements in a variety of ways.