0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to add an element to the matched set:

$(btnNext).add(btnPrev).hover(...);

http://jsfiddle.net/zerkms/qSYXk/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.