Keith Costa
Keith Costa

Reputation: 1793

How to check any element has specific attribute or not

suppose image tag has custom attribute like "originalSrc" or something like this. before accessing that attribute can i check that img tag has that attribute or not by jquery.

$("img.lazyload").each(function() {
$(this).attr("src", $(this).attr("original"));
$(this).removeAttr("original");

});

here i remove attribute like $(this).removeAttr("original"); before remove it i want to check that img tag has the "original" attribute or not. if it has "original" attribute then i will remove it. so if there anything such checking is possible by jquery then please let me know.

thanks

Upvotes: 1

Views: 3739

Answers (4)

mas-designs
mas-designs

Reputation: 7536

Hy, if you want to check if an html Element or attribute is available in jquery simply use that

$('#id[attr]').length

otherwise you could try to get the value of the attribute and then check if its valid

var attribute = $(this).attr('original'); 
if (typeof attribute !== 'undefined' && attribute !== false) { 
//save to other value
} 

Upvotes: 1

Chandresh M
Chandresh M

Reputation: 3828

please try below code..

if ($(this).attr('original')) {
// attribute exists
} else {
// attribute does not exist
}

This would be helpful to you.

Thanks.

Upvotes: 1

Pavan
Pavan

Reputation: 4329

var attr = $(this).attr('original'); 
if (typeof attr !== 'undefined' && attr !== false) { 
//assign to src
} 

Upvotes: 5

aWebDeveloper
aWebDeveloper

Reputation: 38352

 $('#id[attr]').length

This should do the trick

As for as your problem you can just do

 $('img[originalSrc]').length

Better still you can do

$('img[originalSrc]').bind('eventname',function(){

//do something
});

Upvotes: 1

Related Questions