Does the .add() method allow selecting multiple objects in one go instead of adding one at a time?
one.add(two).add(three).add(four).on(\"click\", function()
I'd prefer this array approach, purely for readability...
$([one, two, three, four]).each(function() {
// your function here
});
You cannot add multiple objects in one go, for example:
var groupThree = one.add(three, five); // will not work as expected
You could cache the added objects so that you only have to do the add one time - http://jsfiddle.net/X5432/
var one = $("#1");
var two = $("#2");
var three = $("#3");
var four = $("#4");
var five = $("#5");
var groupOne = one.add(two).add(three).add(four);
var groupTwo = groupOne.add(five);
$('#first').click(function(e){
e.preventDefault();
groupOne.css({'background': '#00FF00'});
});
$('#second').click(function(e){
e.preventDefault();
groupTwo.css({'background': '#FF0000'});
});
But I like the array method better, this is just a different way of thinking about it.
You could put them into an array like this
var k = [one,two,three,four]
$.each(k,function(){
$(this).click(function(){
//Function here
});
});