Categories: jQuery

Select an element with multiple classes in jQuery

Share

The basic concept of jQuery is to select one or more elements using element ID, Class, Attribute, Compound CSS Selector and do something with them. Let’s see how to select an element with multiple classes.

For simple selection you can use following code as below.

Selecting Elements by ID:

$( "#Id" );

Selecting Elements by Class Name:

$( ".ClassName" );

Selecting Elements by Attribute:

$( "input[name='username']" );

Selecting Elements by Compound CSS Selector:

$( "#wrapper ul.list li" );

But, if you want to select an element with multiple classes then using comma you can achieve this.

<p class="Class_1">First Class</p>
<p class="Class_2">Second Class</p>
$( "p.Class_1, p.Class_2" ).css( "border", "2px solid red" );

But, in this case you can get union, not intersection.

If you want an intersection, just put the selectors together without spaces in between them. So, for an element that has an ID of a with classes b and c, you would write:

$('#a.b.c')

Using the filter function you can get this:

$(".a").filter(".b")

Using a Vanilla JavaScript solution you can do the same.

document.querySelectorAll('.a.b')

Read Also:

You can get jQuery reference for multiple selector.

Recent Posts

Unveiling the Truth: Is the Spread of Sinus Infections a Myth or Reality?

Sinus infections, impacting approximately 31 million Americans each year, represent a significant health concern stemming…

18 hours ago

ARTIFICIAL INTELLIGENCE: Advantages And Disadvantages? Everything You Need to Know

Pros And Cons Of AI: Artificial Intelligence directly translates to conceptualizing and building machines that…

1 day ago

Four common mistakes when picking an internet provider

We all live on the internet; we use it for everything. Thus, when it comes…

1 day ago

What Is Commonly Misdiagnosed as Pink Eye: Understanding Eye Conditions and Their Symptoms

Introduction: Pink eye, or conjunctivitis, is a common eye condition characterized by redness and inflammation…

2 days ago

Why Flexible Financing is the Future of Small Business

Small businesses are the backbone of the economy. Still, they often face daunting hurdles when…

2 days ago

Warm Comfort: Choosing the Best Hot Water Bottle for Cozy Nights

Introduction: As the chill of winter settles in or a bout of cold weather strikes,…

3 days ago