Using jquery $ .each with multiple selectors

I want to get all the access keys that are on the button or link. I have the following.

$(":button[accesskey!=''], :a[accesskey!='']").each(function(i) { //code }); 

You can see it here http://jsfiddle.net/QNPZU/

I thought you could have multiple selectors separated by a comma, but the code above does not work.

If i do

 $(":*[accesskey!='']").each(function(i) { //code }); 

it will work, but I suppose there will be a performance problem if dom is huge?

+6
source share
3 answers

You can specify:

 $("button[accesskey], a[accesskey]").each(function(i) { //code }); 
+10
source

Use a instead :a :

 $(":button[accesskey!=''], a[accesskey!='']").each(function(i) { //code }); 

Code: http://jsfiddle.net/QNPZU/3/

+3
source

Check out this updated script: http://jsfiddle.net/techfoobar/QNPZU/2/

There were 2 problems in your code.

a. To select links and buttons, you need to use a and button , not :a and :button

b. You can select items with an attribute called accesskey simply by using a[accesskey] . you do not need to use a[accesskey!=""]

0
source

Source: https://habr.com/ru/post/922916/


All Articles