2

Preamble: I'm Italian, sorry for my bad English.

So this is the question:

considering an HTML object like this:

<div id="myDiv" class="c1 c2 c3 c4 c5"></div>

how can I get an array of strings, using jquery, from the "class" attribute of the "div" element?

Example:

var a = $('#myDiv').getClassArray(); //Not working,just an example

return a;  //[c1,c2,c3,c4,c5]

I thought I could read the "class" attribute and then split the string, but it seems too verbose, I wondered if there was a shorter and more elegant method.

4
  • Don't u think u missed # in $('myDiv').getClassArray(); Commented Jan 4, 2013 at 11:27
  • I dont think there is better way than split string class Commented Jan 4, 2013 at 11:27
  • You could always make a plugin to do this, if it is a frequent need. Commented Jan 4, 2013 at 11:28
  • @ameyarote I forget the '#' character, you're right, but it was just a sample code Commented Jan 4, 2013 at 11:32

3 Answers 3

8

You need # for id selector and use split() to get array from string.

Live Demo

var a = $('#myDiv').attr('class').split(' ');

You can iterate through array using jQuery jQuery.each() of for loop.

var a = $('#myDiv').attr('class').split(' ');

$.each(a, function(index, item){
    alert(item);
});​
Sign up to request clarification or add additional context in comments.

4 Comments

shouldn't it be $('#myDiv') instead of $('myDiv')?
I forget the '#' character,i know, you're right, but it was just a sample code
This solution doesn't account for multiple spaces between class names. $('#myDiv').attr('class').replace(/\s+/g, ' ').split(' '); would take this in to account.
No worries, you can will have array in a
3

The new standard classList property would allow such easy access on DOM elements (e.g., through $('#myDiv')[0].classList in jQuery), but it is not universally implemented. See here for documentation on this standard or here for current browser support.

Providing such functionality as a shim was proposed for jQuery in bug 7378, but was rejected.

You could however, get such support via a shim, such as perhaps https://github.com/eligrey/classList.js

Updated: A demo with the shim is at http://jsfiddle.net/brettz9/WWs5B/1/

3 Comments

This seems to be interesting ... unfortunately, considering that has bugs, I can not use it, but could be useful in the future,tx!
What has bugs? If you are talking about jQuery, that is just a feature request (which was denied), not a bug. What I am talking about is a feature not yet supported in all browsers, but if you add a script tag for the classList.js shim, it should let you use it as I described.
I've updated the answer to link to this demo: jsfiddle.net/brettz9/WWs5B/1
2

Why don't you try this: http://jsfiddle.net/egZ2z/

var cls = $('#myDiv').attr('class');
var n = cls.split(' ')
alert(n);

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.