jQuery: selector (classname with space)

JqueryClassJquery Selectors

Jquery Problem Overview


I'm trying to get a div which has "panel current" as classname. The problem is the space - how can I select it?

Jquery Solutions


Solution 1 - Jquery

Class names can't have spaces in them. What you have there is two classes:

<div class="panel current">

This div has two classes: panel and current. That's easily selected:

$("div.panel.current")...

That means select all divs that have class panel and class current.

Solution 2 - Jquery

$('div').filter(function() {
    return this.className == 'panel current';
});

OR

$("div[class='panel current']");

Use this if you need to match an element with an exact match of class names (including spaces)

The other posters are right, the DiV you posted has two class names: 'panel' and 'current'; If you want to select them both, use $('.panel.current').

This will also include elements like:

<div class="foo panel bar current"></div>

Solution 3 - Jquery

panel current is not a class name, actually it is two class names. You could use the following selector:

$('.panel.current')

Solution 4 - Jquery

The div has two class names:

  • panel
  • current

You can use $("div.panel") or $("div.current") to select it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFuxiView Question on Stackoverflow
Solution 1 - JquerycletusView Answer on Stackoverflow
Solution 2 - JqueryDavid HellsingView Answer on Stackoverflow
Solution 3 - JqueryDarin DimitrovView Answer on Stackoverflow
Solution 4 - JqueryumarView Answer on Stackoverflow