I have two variables defined like this:
var $a = $('#a'),
$b = $('#b');
How can I rewrite the following line using $a and $b?
$('#a, #b').click(function(e){...});
I have two variables defined like this:
var $a = $('#a'),
$b = $('#b');
How can I rewrite the following line using $a and $b?
$('#a, #b').click(function(e){...});
Depends of what you want to do next with your vars.
You could just define them as one:
var $a = $('#a, #b');
$a.click()
Or use them separately:
/* This way the click event is applied even if each selector returns
more then one element. And $a and $b is preserved */
$([].concat($a.toArray()).concat($b.toArray())).click(function () {
console.log(this)
});
EDIT
Updated code.
..fredrik
$a.add($b).click(function(e){...});
add
returns a new node set holding the union. $b can be "pretty much anything that $() accepts."