Reputation: 65
i creating some a-tags and store this in different variable. Now, i want add a hover event to this stored variable.
something like that
var btnPrev = $(document.createElement('a'));
btnPrev.css({
'display':'block',
...
});
btnPrev.text('<');
btnPrev.addClass('issueBtnPrev');
var btnNext = $(document.createElement('a'));
btnNext.css({
'display':'block',
...
});
btnNext.text('>');
btnNext.addClass('issueBtnNext');
now here is the hover event
(btnNext,btnPrev).hover(function() {
$(this).fadeTo(200,'0.3');
}, function() {
$(this).fadeTo(200,'.2');
});
but only the btnPrev have a hover effect is there a way to attach more than one vaiable to a hover effect.
i know i can use $('.issueBtnNext, .issueBtnPrev').hover
Upvotes: 0
Views: 288
Reputation: 165971
You can use the add
method to add another jQuery object (or an element, HTML fragment, etc.) to the current set:
btnNext.add(btnPrev).hover(function() {
$(this).fadeTo(200,'0.3');
}, function() {
$(this).fadeTo(200,'.2');
});
From the jQuery docs on add
:
Given a jQuery object that represents a set of DOM elements, the .add() method constructs a new jQuery object from the union of those elements and the ones passed into the method.
Your current attempt only works for btnPrev
because you are using the comma operator which evaluates both of its operands (which in your case does nothing) and returns the last, which in your case is btnPrev
.
Upvotes: 1
Reputation: 254926
You need to add an element to the matched set:
$(btnNext).add(btnPrev).hover(...);
http://jsfiddle.net/zerkms/qSYXk/
Upvotes: 1