views:

92

answers:

4
$( "input[role=submit_action], button[role=submit_action], div[role=submit_action], span[role=submit_action], a[role=submit_action]").live(  "click", function() {
});

and this too:

$( "input[role=submit_action], input[role=submit_require]").live(  "click", function() {
    if ($(this).attr('role') == "submit_action") {
            // do this...
    }
    else {
            // do this...
    }
});

For example an abbreviation could be:

$("input[role=(submit_action|submit_require)]"
A: 

Try:

$( "[role=submit_action]").live("click", function() {
 //...
});

for the second one, are you trying to use both on the same page?

fudgey
+2  A: 

I wouldn't change the first one as it's efficient the way it is. It can be made shorter using:

$('[role=submit_action]')

..but that would require the traversal of every element in DOM which will slow it down significantly. You can use starts with selector in the second example, that might still be allright:

$('input[role^=submit_]')...

Anyways, if it isn't broken, don't fix it. :)

Tatu Ulmanen
That's slow ............
Cris Hong Kong CRISHK
@Cris: How much HTML do you have exactly?! I'm having trouble believing this is a bottleneck.
Mark
this is good, but what about INPUT, DIV, SPAN, A ???: $('(input|span|a|div)[role^=submit_]') ----> Like this... It's possible?
Cris Hong Kong CRISHK
+4  A: 

I'd use .delegate() here for efficiency (to not traverse every DOM element executing the selector initially), like this:

$(document.body).delegate("[role=submit_action]", "click", function() {
  //do stuff
});

The difference here is that unlike .live(), it's a lot cheaper on startup, since $("role=submit_action]") (even before a .live() call) has to iterate over all the elements in the DOM checking for the role attribute, you bypass this expense with .delegate().

Since performance is a concern, don't take my word for it, test it yourself here:
http://jsperf.com/live-vs-delegate-test

Nick Craver
That's slow ............
Cris Hong Kong CRISHK
@Cris - It's *very* fast, in fact much faster than any of the other approaches listed here, are you saying it's slow because you've ready it somewhere, or because you've tested it? :)
Nick Craver
I tested it... What's more fast? $('.searchme') or $('[role=searchme]') ??? the second option is more fast, you know it, but I think is very more fast: $('input[role=searchme]')... what do you think?
Cris Hong Kong CRISHK
@Cris - It depends on your browser and which elements are in your DOM (for example are they mostly input? how much does that narrow it down?), but `.delegate()` doesn't execute the selector at all, so your comparisons are a moot point... **no selector** is faster than **any** selector, always.
Nick Craver
@Cris - you must realize that the selector in `.delegate()` is not checked until an event actually happens, and then it's only checked against the *single* element that is the event target. That's why `.delegate()` should be the way you choose to do this. Nick is definitely correct here (as he is almost everywhere :-)
Pointy
+2  A: 

Provided that no other elements may have this role, you can write the selector as

$( "[role=submit_action]").live(  "click", function() {});

However, I wouldn't, as it makes quite an expensive search before it binds the function. Use delegate instead:

$('body').delegate('[role=submit_action]','click',function() {});

This is functionally equivalent, except that it does not require an expensive DOM traversal before binding the handler.

lonesomeday