3

I'm using jQuery to loop through some elements in a document. These elements are of type <tr> with class .input-row. Some of the elements can also have a secondary class (for example .input-area).

I use $(".input-row").each() to loop through the elements, but how can I determine if the $(this)-element has a "secondary" class and if so, get the name of this?

James Allardice
  • 164,175
  • 21
  • 332
  • 312
Christian Tang
  • 1,450
  • 4
  • 19
  • 30
  • use split. see: http://stackoverflow.com/questions/4239947/jquery-get-second-class-name – Kai Sternad Oct 28 '11 at 07:44
  • If you need to *check* the class of an element you might be doing something wrong. Nowadays `class` is not meant to store data anymore - there is `data-whatever` for it (available via jQuery `.data('whatever')`). If you already need the class to apply formatting a check using `hasClass()` is absolutely fine though. – ThiefMaster Oct 28 '11 at 07:50

6 Answers6

4
$(".input-row").each(function(){
  $.each($(this).attr('class').split(/\s+/), function(i, v){
    if(v !== 'input-row'){
      alert(v);
    }
  });
});
michaeljdennis
  • 602
  • 1
  • 7
  • 12
  • 2
    The question asks "how can I determine if the $(this)-element has a "secondary" class and if so, get the name of this"... your example means you already know the name of the class – James Allardice Oct 28 '11 at 07:47
2

You can get the list of all classes using attr, and you could simply get rid of the the class you used to select the elements:

$(".input-row").each(function() {
    var allClasses = $(this).attr("class"),
        otherClasses = allClasses.replace("input-row", "");  
});

If the element has more than two classes, then you will be left with a list of the other classes. To separate them, you could split on the spaces.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

You can get all the classes on the element and then remove the one you know exists('input-row'), then you will be left with any other classes on that element.

$('.input-row').each(function() {
    var classes = $.trim($(this).attr('class').replace('input-row', ''));
    if (classes.length > 1) {
        alert(classes) // Names of all of the remaining classes
    }
}

If you want to get them individually, you can do classes.split(' ') to get an array.

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

I prefer a declarative approach. You know which classes there can be, so write cases for them:

$(".input-row.input-area").each( /* ... */ );
$(".input-row.input-field").each( /* ... */ );

If you for some reason don't know the class name, you can extract it along the lines of:

$(".input-row").each(function () {
  var myClasses = this.className.split(" ");
  $.each(myClasses, function (i, className) {
    if (className != 'input-row') {
      alert(className); 
    }
  });
});
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

You could also do something like this to detect the number of classes:

var nc = $('#myId').attr('class').split(' ').length;

Then if nc is greater than 1:

$('#myID').attr('class').split(' ')[1]

should return the 'secondary' class

See a fiddle

Community
  • 1
  • 1
m90
  • 11,434
  • 13
  • 62
  • 112
0

I'd go for this

var otherClasses = $('.input-row').attr('class').split(' ');
otherClasses.splice($.inArray("input-row", otherClasses),1); 

Simply doing this

var otherClasses = $('.input-row').attr('class').replace('input-row', '').split(' ');

Will give you an extra string in the array with an empty value in it, because of the space behind "input-row"

Matthew Grima
  • 1,513
  • 5
  • 25
  • 40