0

I have a div having many classes , let suppose

<div class="myDiv one two three"></div>

Now I want to get the class names using the class "myDiv", here are some cases , in some cases I need the class name which is on third position i.e "two", In some times I need class on position second which is "one", can any one have idea to get my desired classes. My jQuery code is

alert($('.myDiv').attr('class'));

But this alert all the classes.

5
  • 2
    The order of class names can change if manipulated by script... so it might be better to rethink the solution Commented Aug 4, 2015 at 8:04
  • Try splitting by an empty space: ' ' Commented Aug 4, 2015 at 8:05
  • I assume once you have the required class, you ought to do something with the element? try .hasClass(). So if ($('.myDiv').hasClass('one')) { // do Something }. Commented Aug 4, 2015 at 8:05
  • required class is myDiv. Commented Aug 4, 2015 at 8:06
  • the others classes are dynamic , there for I want to get their names. Commented Aug 4, 2015 at 8:07

4 Answers 4

4

If you can accept use dom elements, it has classList which may be what you want.

  • Element.classList

elementClasses is a DOMTokenList representing the class attribute of elementNodeReference. If the class attribute was not set or is empty elementClasses.length returns 0. element.classList itself is read-only, although you can modify it using the add() and remove() methods.

alert($('.myDiv')[0].classList[2]);
// Get the count of the classes
var length = $('.myDiv')[0].classList.length;

// Get last class from the list.
alert($('.myDiv')[0].classList[length - 1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="myDiv one two three"></div>

Sign up to request clarification or add additional context in comments.

6 Comments

Remember that classList is available at IE10+ only.
I need to check it on only ff, chrome and safari.
I think there's a polyfill that will make it work lowest to IE8.
How would I get the last class name with out knowing the number of classes? any idea
classList has read-only attr length, so you can use length - 1 to get the last element.
|
1

Try hasClass

$( ".mydiv" ).hasClass( "foo" );

Comments

1

$(function(){
  
  var classNames = $('.myDiv').attr('class').split(" ");
  
  alert(classNames[0]);
  alert(classNames[1]);
  alert(classNames[2]);
  alert(classNames[3]);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myDiv one two three"></div>

You can do something like this. I dont think there is any way of getting class names by index.

Comments

0

What's wrong with String.split(' ') ?

var classes = $('.myDiv').attr('class').split(' ');
alert(classes); // myDiv,one,two,three

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.