Search code examples
jquery.netaccess-keys

Jquery select all elements that have an accesskey defined


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


Solution

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