Note that there are some explanatory texts on larger screens.

plurals
  1. POSome JQuery code shortcuts
    primarykey
    data
    text
    <p>I'm writing a lot of JQuery code recently that seems to repeat itself and I'm wondering if there are any shortcuts for the following:</p> <p>1) Selecting multiple elements by name, i.e.</p> <pre><code>$("input[type=text][name=one][name=two][name=three]").live(); &lt;-- is that correct syntax </code></pre> <p>2) Multiple live bindings, i.e.</p> <pre><code>$("input[type=text]").live('blur and click',function(){}); </code></pre> <p>Thanks</p> <p><b>Edit</b>:</p> <p>I've also got this use case, where I want to flip display on multiple elements, I found this on this forum, but I haven't tested it yet. Here's my impl and I'd welcome feedback on the strategy</p> <pre><code>$("#btn_account_edit").live('click',function(){ flip_display_on_off( $("#btn_account_edit_cancel, #btn_account_save, #account_edit_form"), $("#btn_account_edit, #account_edit_static"), false,true); }); $("#btn_account_edit_cancel").live('click',function(){ flip_display_on_off( $("#btn_account_edit_cancel, #btn_account_save, #account_edit_form"), $("#btn_account_edit, #account_edit_static"), true,false); }); </code></pre> <p>   </p> <pre><code>function flip_display_on_off($element_set1,$element_set2,flip1,flip2) { var display1 = 'block' ? if flip1 : 'none'; var display2 = 'none' ? if flip2 : 'block'; for (i =0; i &lt; element_set1.length; i++) { $element_set1[i].css('display',display1); } for (i =0; i &lt; element_set2.length; i++) { $element_set2[i].css('display',display2); } } </code></pre> <p><b>Second Edit</b>:</p> <p>This is the final version of the code, tested and works perfectly! Thanks to @thecodeparadox</p> <pre><code>$("#btn_account_edit").live('click',function(){ flip_display_on_off( $("#btn_account_edit_cancel, #btn_account_save, #account_edit_form"), $("#btn_account_edit, #account_edit_static"), true); }); $("#btn_account_edit_cancel").live('click',function(){ flip_display_on_off( $("#btn_account_edit_cancel, #btn_account_save, #account_edit_form"), $("#btn_account_edit, #account_edit_static"), false); }); function flip_display_on_off($element_set1, $element_set2, flip1) { $element_set1.css('display', flip1 ? 'block' : 'none'); $element_set2.css('display', flip1 ? 'none' : 'block'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload