This is not true. If classes are separated by more than one space, and you get a second class, you become empty. Example:
<div class="something fooBar"></div> var classes = $('div').attr('class').split(' '); alert('classes: ' + classes + '; length: ' + classes.length );
I am using the following code:
function getNumClass(el, num) { var classes = el.split(' '); var newclasses = []; var ret; for (var i = 0; i < classes.length; i++) { ret = $.trim(classes[i]); if(ret.length > 0) { newclasses.length += 1; newclasses[newclasses.length-1] = ret; } } if (num > newclasses.length) return false; return newclasses[num - 1]; }
skeef Jan 07 '16 at 17:13 2016-01-07 17:13
source share