www.jensolsson.se www.jensolsson.se - 4 months ago 14
jQuery Question

Identify if a particular DOM object is actually a slider object

I am using a JQuery slider and it works great, however for a particular function I need to find out of a particular object is a slider, if so I would like to perform some specific code.

The slider in my HTML form is named mySlider. I would expect to be able to do something like this:

if($("#mySlider").type() === "slider") {
alert("Yes, a slider it is");
}
else {
alert("No, this is not a slider :(");
}


I have tested .type(), .is("slider"), .is("rangeslider"), .is("input:slider"), .role() but none of them seem to do what I need.

Answer

The usual solution here if the plugin doesn't expose a method for this (which is sadly rare) is to look for something that the plugin adds to the element. In this case, it looks a lot like it adds a ui-slider-input class. So:

if ($("#mySlider").hasClass("ui-slider-input")) {
    // Yes
} else {
    // No
}