javascript - Bind event to multiple jquery elements simultaneously -


i need bind same event (for example click) n jquery elements. know can achieve result selector:

$("#first, #second, .third").on("click", function(){}); 

but if, instead of selector, want use list of jquery elements?

this solution i'm using right now, assume $first , $second result of function:

var $first = $("#first"); var $second = $("#second");  $([$first[0], $second[0]]).on("click", function(){     alert("click " + this.innertext) }); 

find fiddle play here: https://jsfiddle.net/0z2r55d6/

as can see need extract, each jquery element, html element , push array. find pretty ugly , unhandy.. there better way achieve same result? didn't find in jquery's documentation.

thanks

using jquery add() method:

$first.add($second).add($third).on('click', handler); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -