Possible Duplicate:
Get Class List for Element with jQuery
If I have something like this, I can get the id of the clicked selector by doing this.
$('#one, #two, #three').click(function(){
alert(this.id)
})
My question is if I have classes instead of id’s, how do i get the classname of the clicked selector.
$('.one, .two, .three').click(function(){
// ??????
})
EDIT:
The clicked elements may have multiple classes, and I wish to isolate the class that was used to initially select the element.
You’d use the native
.classNameproperty.It’s
this.classNameinstead ofthis.classbecauseclassis a reserved word in JavaScript, and some browsers haven’t allowed reserved words to be used as property names.It sounds like there could be multiple classes on an element. To isolate one, you can use
$.grepwith$.inArray.DEMO: http://jsfiddle.net/rFT8j/
Or you could use
$.eachinstead of$.grep.DEMO: http://jsfiddle.net/rFT8j/1/
If you want something a little simpler, one solution would be to take advantage of closures, and assign separate handlers that each reference a different name in the variable scope…
DEMO: http://jsfiddle.net/rFT8j/3/