Mircea
Mircea

Reputation: 11623

jQuery check if element contains any attribute

I can check if an element have a specific attribute with:

if ($('#A').attr('myattr') !== undefined) {
    // attribute exists
} else {
    // attribute does not exist
}

How can I check if an element have any attribute?

Thank you

Upvotes: 6

Views: 17308

Answers (7)

cherouvim
cherouvim

Reputation: 31928

You'd need to use information from Get all Attributes from a HTML element with Javascript/jQuery

Upvotes: 2

Neo Zhou
Neo Zhou

Reputation: 61

$("selector").get(0).hasAttributes("attributes");

Upvotes: 4

jarodtaylor
jarodtaylor

Reputation: 571

I'm not sure if you're wanting to just return the elements that contain the attribute, but if you are, I've found the easiest method to be:

$('selector[attribute]')

That will return the jquery object to include all elements that contain the attribute regardless of its value. For example.

$(':text[maxlength]').addClass('has_max_length');

This would give all text inputs with the maxlength attribute a class of 'has_max_length'.

Upvotes: 1

Melanie
Melanie

Reputation: 1208

Here's a solution that I found to be more reliable to know if an element has defined attributes that works in IE, Chrome and Firefox:

$(selector).each(function(index, element) {
    if (element.hasAttributes) { // Some browser supports this method
        if (element.hasAttributes()) {
            return true;
        }
     } else {
           // IE counts many attributes - even if not set explicitly
           var attrs = element.attributes;
           for (var i = 0; i < attrs.length; i++) {
                if (attrs[i].specified) {
                    return true;
                }
            }
     }
     return false;
});

The problem is that IE returns high attributes count even when none is set explicitly. For example, for a simple < p > tag, IE was telling me it had 111 attributes. With that solution, I was able to filter out those attributes that I wasn't interested in.

This solution was taken here: http://help.dottoro.com/ljevmnkf.php (just copied it in case the page is removed!)

Upvotes: 0

Jeff Sternal
Jeff Sternal

Reputation: 48675

Here's a function that determines whether any of the elements matching a selector have at least one attribute:

function hasOneOrMoreAttributes(selector) {
    var hasAttribute = false;
    $(selector).each(function(index, element) {
        if (element.attributes.length > 0) {
            hasAttribute = true;
            return false; // breaks out of the each once we find an attribute
        }
    });
    return hasAttribute;
}

Usage:

if (hasOneOrMoreAttributes('.someClass')) {
    // Do something
}

If you want to operate on selected elements that have at least one attribute, it's even easier - you create a custom filter:

// Works on the latest versions of Firefox, IE, Safari, and Chrome
// But not IE 6 (for reasons I don't understand)
jQuery.expr[':'].hasAttributes = function(elem) {
    return elem.attributes.length;
};

Which you can use like this:

$(document).ready(function(){
    $('li:hasAttributes').addClass('superImportant');
}

Upvotes: 6

Tarik
Tarik

Reputation: 81831

Not a whole jQuery code but it will work

$("a").click(
function () {
    if($("a").get(0).attributes > 0)
        alert("it has attributes");
})

Upvotes: 2

Pointy
Pointy

Reputation: 414086

If you want to see if the element has a particular attribute, just do this:

if ($('#something').is('[attribute]')) {
  // ...
}

Upvotes: 9

Related Questions