The 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
$('div').has('span');
The following code selects all <p>, <h3>, and <div> elements that have a <span> element inside of them:
Code snippet
$('p, h3, div').has('span');
The following code selects a <span> element that has a hyperlink:
Code snippet
$('span').has('a');
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
$('div').has('span, a');
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:
To select all <div> elements that have a child <span> element with the text "Hello, world!", use the following code:
Code snippet
$('div').has('span:contains("Hello, world!")');
To select all <div> elements that have a child <span> element with a class of "my-class", use the following code:
Code snippet
$('div').has('span.my-class');
To select all <div> elements that have a child <span> element that is a descendant of a <ul> element, use the following code:
Code snippet
$('div').has('ul > span');
The has() method is a versatile tool that can be used to select elements in a variety of ways.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The 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.