How do you check if there is an attribute on an element in jQuery? Similar to hasClass
, but with attr
?
For example,
if ($(this).hasAttr("name")) {
// ...
}
How do you check if there is an attribute on an element in jQuery? Similar to hasClass
, but with attr
?
For example,
if ($(this).hasAttr("name")) {
// ...
}
var attr = $(this).attr('name');
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== 'undefined' && attr !== false) {
// ...
}