2

I want to select all elements that have an accesskey defined on them.

I know that i can do this: $('[accesskey]'), but that gives me lot of inputs, hrefs etc, on the page (most of them have an empty accesskey).

What is the way to select only the elements where the accesskey actually has a value?

Edit: I found the cause of the empty accesskeys as well, was caused by some old disable / restore accesskeys javascript functions over multiple modal dialogs.. normally you won't get that much elements with empty accesskeys

Erik Dekker
  • 2,395
  • 5
  • 33
  • 55
  • You can check this post... http://stackoverflow.com/questions/1097522/check-existence-of-an-attribute-with-jquery – Subhajit Jun 15 '12 at 09:32
  • @Subhajit: that post is about the existance of an attribute. My question is about an attribute that should have a value, not just the existance of it. – Erik Dekker Jun 15 '12 at 10:26

3 Answers3

2

With one selector:

$('[accesskey][accesskey!=""]').foo

How does it work:

// Has the accesskey attribute defined.
[accesskey]

// Doesn't have an empty value for the accesskey attribute.
[accesskey!=""]

Together it select every element which has the accesskey attributes and it's not empty.

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

You can do this

$('[accesskey]').filter(function(){
    return $(this).prop('accessKey');
});

.filter() or like other have already said you can use the attribute-not-equal-selector

Working sample

Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
  • @thecodeparadox, Thanks. `attr` is fixing that. I thought `attr` and `prop` is interchangeable. Can you please correct me? – Prasenjit Kumar Nag Jun 15 '12 at 09:54
  • @thecodeparadox. By the way, the problem was with your fiddle, `prop` works just fine... Please don't tell people they are "totally wrong" specially when they are not. :( And joy, you do need to change `accesskey` to `accessKey` if you're using `prop`, properties are case sensitive while attributes are not. Good luck for you both... – gdoron Jun 15 '12 at 10:12
0

you can use and additional loop

$("[accesskey]").each(function() {
    if($(this)).attr('accesskey').length > 0) {
        // do it
    }
}

i hope it will help you