views:

659

answers:

4

Hi, 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

A: 

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

if ($('#something').is('[attribute]')) {
  // ...
}
Pointy
I am interested to see if an element have any attribute at all not a specific one.
Mircea
OK, but the point is that what you typed in your example above won't work.
Pointy
Oh, Ok I understand your point... Thanx
Mircea
+1  A: 

You'd need to use information from http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery

cherouvim
Thanx, I will try that
Mircea
+1  A: 

Not a whole jQuery code but it will work

$("a").click(
function () {
    if($("a").get(0).attributes > 0)
        alert("it has attributes");
})
Braveyard
Thanx Tarik, I am looking into this
Mircea
+3  A: 

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');
}
Jeff Sternal
Isn't it a bit too much : `$("some").get(0).attributes > 0` would be enough I think.
Braveyard
@Tarik - that only checks the first matched element, when there may be multiple matches.
Jeff Sternal
@Jeff - Is there any way to add a class on the elements that have attributes with this function?Thanx again
Mircea
@Mircea - I updated my answer to show how you might do this, though it's buggy in IE 6 for reasons I can't figure out right now.
Jeff Sternal
Thanx again Jeff
Mircea
But wasn't he asking "How can I check if an element have any attribute?". So I think it should be fine :) So if an element has more than 0 attribute, then it has at least one attribute.
Braveyard
@Tarik - sorry, I wasn't very clear the first time. You're right that the question asked "How can I check if an element have any attribute?" but jQuery doesn't guarantee that it will return a single element - it's always possible that it will match zero or multiple elements, so the safest thing to do is check all of them (or none of them, since element zero may not exist).
Jeff Sternal