6

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

2
  • I wonder if jQuery offers something like $("#a[*]")? Commented Feb 10, 2010 at 22:44
  • @Naeem : I think it could be such a nice feature but since there are easy ways to solve this problem, I don't think they could add it. Commented Feb 14, 2010 at 0:19

7 Answers 7

9

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

if ($('#something').is('[attribute]')) {
  // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am interested to see if an element have any attribute at all not a specific one.
OK, but the point is that what you typed in your example above won't work.
This can work easily with multiple attributes too, just comma seperate the attribute selector in the 'is' method: ".is('[attribute], [attr2], [att3]')"
6

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');
}

6 Comments

Isn't it a bit too much : $("some").get(0).attributes > 0 would be enough I think.
@Tarik - that only checks the first matched element, when there may be multiple matches.
@Jeff - Is there any way to add a class on the elements that have attributes with this function? Thanx again
@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.
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.
|
4
$("selector").get(0).hasAttributes("attributes");

Comments

2

Not a whole jQuery code but it will work

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

1 Comment

Thanx Tarik, I am looking into this
2

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

Comments

1

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'.

Comments

0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.