Skip to content

Instantly share code, notes, and snippets.

@plbabin
Created April 7, 2011 18:47
Show Gist options
  • Save plbabin/908413 to your computer and use it in GitHub Desktop.
Save plbabin/908413 to your computer and use it in GitHub Desktop.
Small custom jQuery selector that validate if an element has a data attach to himself
$.extend($.expr[':'], {
hasData: function (obj, intStackIndex, arrProperties) {
var arguments = arrProperties[ 3 ];
var $this = $(obj);
var isValid = false;
if(arguments.indexOf('=') > -1){
arguments = arguments.split('=')
if($this.data(arguments[0]) == arguments[1]){
isValid = true;
}
}else{
if($this.data(arguments)){
isValid = true;
}
}
return isValid;
}
});
//exemple
$('body:hasData("map")');
$('body:hasData("map=4")');
$('body:hasData("events")'); //return true if the element has some events binded to him
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment