jQuery Tutorial Tutorials - jQuery .has() method

jQuery .has() method

The .has() method in jQuery is used to filter the set of matched elements, keeping only that have a descendant that matches the selector or DOM element.

jQuery .has() method Syntax

.has(selector)
.has(contained)

selector is an expression and contained is a DOM element to match elements against.

.has(selector)

example

Highlight the item that have a class selected

$('li').has('.selected').css('background','cyan');

Try now

.has(contained)

example

Add a red background for the item that has a <ul> among its descendants.

$( "li" ).has( "ul" ).css( "background-color", "red" );

Try now

Date:2019-08-26 13:39:28 From:www.Lautturi.com author:Lautturi