May 27, 2013
How to determine whether it was the class or id selector clicked in joint event handler?
Sephiith’s Question:
In the below event handler I need to determine whether it was ID or Class that was clicked and assign a variable based on that.
What would be the easiest way of determining that in an IF statement?
jQuery CODE:
$(document).on('click', '.inter [class], .inter [id]', function () {
prevClass = className;
IF CLASS >>>>>>> className = this.className.substring(1);
IF ID >>>>>>>>>> className = this.id.substring(1);
var back = '<div id="_'+ prevClass +'"></div>';
link[prevClass] = original;
original = link[className];
link[className] += back;
$('.inter').fadeTo(250, 0.25, function () {
$('.inter').html(link[className]);
$('.inter').css({'background-image': 'url("' + className + '.png")'});
$('.inter').fadeTo(250, 1.00);
});
});
});
Something like this will work: (this.id || this.className).substr(1)
This works
IF this.className.substring(1) !== "" className = this.className.substring(1);
IF this.id.substring(1) !== "" className = this.id.substring(1);