Reputation: 2207
I am building a jQuery plugin and i'm using a variable in it which can be an class or id. I dont want to use the . or # in the variable, so is there an way to check if an variable is a class or id.
Coudn't find anything on the web about this.
some of the code
var defaults = {
trigger: ''
};
var opt = jQuery.extend(defaults, opt);
jQuery(opt.trigger).click(function(){
//run code
});
Upvotes: 1
Views: 1031
Reputation:
Something like this?
if(document.getElementsByClassName(your_vairable).length != 0){
// It's a class
}
else if (document.getElementById(your_variable) != null){
// It's an id
}
else{
// Oh god what have you done
}
Upvotes: 3
Reputation: 24052
No, unless you scour the CSS on the page and match it to a class, but who's to say you can't identify something the same as a class name?
I would do something like (this is a function but you can apply to a plugin):
function passIdentifier(id, isClass)
{
var selector;
if (isClass)
{
selector = '.' + id;
}
else
{
selector = '#' + id;
}
}
Upvotes: 0