steve Kim steve Kim - 2 months ago 6
Javascript Question

Check the selected class in jQuery

I have following function:

function next_or_previous(){
$(document).on('click', 'a.next, a.previous', function (e) {
var rh = $(this);

if (rh == 'a.next') { //??
console.log("next");
} else if (rh == 'a.previous') {
console.log("previous");
}
});
};


I simply want to check what class (
next
or
previous
) was clicked. How would I go about doing this?

Answer

can use hasClass() or is()

if (rh.hasClass('next') ){...

if you only have 2 selectors you shouldn't really need to check in the else

Comments