1

I want to change the background color of a button on hovering in and out of it. Please help me to make my jquery code work.

function onHoverIn(button) {
    $(button).css('background-color', 'rgba(193, 86, 10, 0.86)')
};
function onHoverOut(button) {
    $(button).css('background-color', 'rgba(26, 13, 3, 0.26)')
};
$("button").hover(onHoverIn(this), onHoverOut(this));

Reference - https://api.jquery.com/hover/

3 Answers 3

5

The callback hover() functions are already good friends with the this (jQuery Element Object reference), so all you need is to revoke the $(this)

function onHoverIn() {
   $(this).css('background-color', 'rgba(193, 86, 10, 0.86)');
}
function onHoverOut() {
   $(this).css('background-color', 'rgba(26, 13, 3, 0.26)');
}
$("button").hover(onHoverIn, onHoverOut);

You cannot pass arguments to arguments. That's what was wrong in your code:

.method( myFunz( this ) )
//       ^argument  ^argument :( 

How's that $(this) is available in those Named Function Declarations?

jQuery .hover() Docs

.hover( handlerIn, handlerOut )

handlerIn
Type: Function( Event eventObject )
A function to execute when the mouse pointer enters the element.

handlerOut
Type: Function( Event eventObject )
A function to execute when the mouse pointer leaves the element.

so .hover() method accepts two function callbacks.

and exploring the jQuery code for the .hover() method:

hover: function( fnOver, fnOut ) {
    return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
},

you can clearly see that it returns this (the Event eventObject) to maintain methods chainability (.hover().click().etc()) and to make for you accessible the this (Event eventObject) who triggered the event.


If (really, if) all you need is a :hover that changes a background-color

use CSS :)

button {
    background-color : rgba(193, 86, 10, 0.86);
}
button:hover {
    background-color : rgba(26, 13, 3, 0.26);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Can you please tell me what is wrong with my code ?
Have a demo, if you'd like: jsfiddle.net/davidThomas/sbxnf6p6 (I was a few seconds behind you because I wanted to verify first...darn!) ;)
Thanks again. How is onHoverXxx method given a reference to the button ?
@HelloWorldNoMore a function is a function, so it's given the exact way as you'd do using .hover(function(){ $(this).blabla(); }); where this is always this (the element who triggered the event).
@HelloWorldNoMore to explain a bit more, jQuery Methods like .hover(), .css(), .click() etc... name it (almost all methods, related to HTMLElements) internally, (to maintain Methods chainability) by default return this;, so that the this reference can be passed trough methods and accessed inside the function itself (without the need to specify some i.e: that argument in the fn arguments list.).
|
0

Here is another way. When jQuery fires the callback, it will be called on the button object. so this will be the button.

function onHoverIn(button) {
    $(button).css('background-color', 'rgba(193, 86, 10, 0.86)')
};

function onHoverOut(button) {
    $(button).css('background-color', 'rgba(26, 13, 3, 0.26)')
};


$("button").hover(function() {
    onHoverIn(this)
},function() {
    onHoverOut(this)
});

1 Comment

Thanks for your answer.
0

Another option is to bind the mouseover and mouseout events to be more explicit with your event bindings. JSFiddle example.

function onHoverIn() {
   $(this).css('background-color', 'rgba(193, 86, 10, 0.86)');
}

function onHoverOut() {
   $(this).css('background-color', 'rgba(26, 13, 3, 0.26)');
}

$(document).on('mouseover', 'button', onHoverIn);
$(document).on('mouseout', 'button', onHoverOut);

1 Comment

Thanks for your answer.

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.