My jquery code
It's not working when
$("a").on("hover",function(){$(this).css("background","#ccc");},function(){$(this).css("background","#fff")});
But is working when
$("a").hover(function(){$(this).css("background","#ccc");},function(){$(this).css("background","#fff")});
How to make it to work with hover
In case of .on() hover it will look like
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// code for mouseenter
} else {
// code for mouseleave
}
});
But for .hover() is accept two functions first one for mouseenter and second one for mouseleave.
$('a').hover(
// for mouseenter
function() {
},
// for mouseleave
function() {
}
);
So if you want to use .on() then your code will:
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// code for mouseenter
$(this).css("background","#ccc");
} else {
// code for mouseleave
$(this).css("background","#fff")
}
});
As #ThiefMaster comment if you want to bind mouseenter and mouseleave separately then you can try:
$('a')
.mouseenter(function() {
$(this).css('background', '#ccc');
})
.mouseleave(function() {
$(this).css('background', '#fff');
});
or using .on() you can do
$('a').on({
mouseenter: function() {
$(this).css('background', '#ccc');
},
mouseleave: function() {
$(this).css('background', '#fff');
}
});
Here's a live demo
And the code
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// do something when mouse enter
alert("mouse enter");
} else {
// do something when mouse leave
alert("mouse leave");
}
});
Hover is an shortcut for mouseenter and mouseleave events. So you can bind those using on like
$("a").on({
mouseenter: function(){$(this).css("background","#ccc");},
mouseleave: function(){$(this).css("background","#fff");}
});
Related
Is it possible listen click and change for one code?
$(document).on("click", "button.options_buy",function(event) {
// same code
}
$(document).on("change", "select.options_buy",function(event) {
// same code
}
I try this
$(document).on("click change", "button.options_buy,select.options_buy",function(event) { }
It works but I want 'click' only for 'button.options_buy' and 'change' for 'select.options_buy'
is it possible?
Best way to do it is to have two event handlers as you have, but only have a common function that is called from each:
$(document).on("click", "button.options_buy",function(event) {
commonFunction();
})
$(document).on("change", "select.options_buy",function(event) {
commonFunction();
})
function commonFunction(){
//common function code
}
I would like to extend your code.
$(document).on("click change", "button.options_buy,select.options_buy",function(event) {
if(event.type=="click"){
someFunction();
} else if(event.type=="change"){
someFunction();
}
}
You can use .on() to bind a function to multiple events:
$('#foo').on('keypress click change', function(e) {
//
});
OR declare a function and call it for each event
$('#foo')
.change(myFunction)
.click(myFunction)
.blur(myFunction)
jQuery .bind()
$( "#foo" ).bind({
click: function() {
// Do something on click
},
mouseenter: function() {
// Do something on mouseenter
}
});
OR
$( "#foo" ).bind( "mouseenter mouseleave", function() {
$( this ).toggleClass( "entered" );
});
(function() {
var bgOn;
$(".ContainingBox").on('hover', function() {
function() {
bgOn = $(this).css("background-color");
$(this).css("background-color", "#e5fff8");
}, function() {
$(this).css("background-color", bgOn);
}
});
})();
I want to bind an event for hover. This code worked fine when I did not wrap it in the anonymous function, and used .hover() . However, we have a requirement to not use global variables. So i need to bind the event!
Is this not possible?
Your code has syntax errors, specifically on() does not accept multiple callbacks etc.
Also, there is no native hover event, you should use mouseenter and mouseleave instead
$(".ContainingBox").on({
mouseenter: function() {
$(this).data('bg', $(this).css("background-color"));
$(this).css("background-color", "#e5fff8");
},
mouseleave: function() {
$(this).css("background-color", $(this).data('bg'));
}
});
Using jQuery's data(), and not a single variable, will remember the background color for each element
FIDDLE
try this....
(function() {
var bgOn;
$(".ContainingBox").on('mouseover', function() {
function() {
bgOn = $(this).css("background-color");
$(this).css("background-color", "#e5fff8");
}, function() {
$(this).css("background-color", bgOn);
}
});
})();
I have this code which is working great on already created divs
$('.MyDivs').click(function(){
$('#OtherDiv').css('display','block');
}).mouseleave(function(){
$('#OtherDiv').css({ display: 'none' });
});
but does not work on dynamically created new Divs. I know there is a .on method of Jquery for dynamically created divs but do not know how to bind 2 events with it. I tried something like this
$(document).on('click', '.MyDivs', function()
{
$('#OtherDiv').css('display','block');
}).mouseleave(function(){
$('#OtherDiv').css({ display: 'none' });
});
I also tried this
$(document).on('click', '.MyDivs', function()
{
$('#OtherDiv').css('display','block');
});
$(document).on('mouseleave', '.MyDivs', function()
{
$('#OtherDiv').css({ display: 'none' });
});
but does not work. How can I bind click and mouseleave methods with .on?
My Problem
I just want to show Otherdiv on click of any div which has .MyDivs class and hide Otherdiv when mouse leaves currently .MyDivs div
you can try this. This should work.
$(document).on({
mouseenter: function() {
// Handle mouseenter...
},
mouseleave: function() {
// Handle mouseleave...
},
click: function() {
// Handle click...
}
}, ".MyDivs");
You need to use on for each chain. This should work:
$(document).on('click', '.MyDivs', function() {
$('#OtherDiv').css('display','block');
}).on('mouseleave', '.MyDivs', function() {
$('#OtherDiv').css('display','none'});
});
Or you can bind both events and look for the event.type, then toggle the div depending on the result:
$(document).on('click mouseleave', '.MyDivs', function(e) {
$('#OtherDiv').toggle( e.type == 'click' );
});
I'm changing my codes to be compatible with jQuery 1.8 and I'm stuck with this hover which doesn't work. When I used then same thing with a click it worked. Here is my code, can anyone tell me where I'm going wrong?
$(document).on('hover', '.top-level', function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}, function () {
$(this).find('.dropfcnt').hide('blind', function () {
$('.actionfcnt').hide();
});
});
Deprecated as of jQuery 1.8: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.
Source: http://api.jquery.com/on/#additional-notes
That pretty much says it all, you cant use "hover" for that:
$(document).on('mouseenter','.top-level', function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}).on('mouseleave','.top-level', function(){
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
there is no "hover" event.
there is .hover() function that takes 2 callbacks (as in your example).
Try:
$(".top-level").on({
mouseenter: function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
},
mouseleave: function (event) {
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
}
});
OR
$(".top_level").on("hover", function(event) {
if(event.type == "mouseenter") {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}
else if (event.type == "mouseleave") {
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
}
});
.on function has only 3 parameters : http://api.jquery.com/on/
If you don't need your handlers be bound to dynamically added elements as well, then you may use the good old hover function with 2 event handlers.
$('.top-level').hover(function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}, function (event) {
$(this).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
By the way, $(selector).hover(handlerIn, handlerOut) is shorthand for $(selector).mouseenter(handlerIn).mouseleave(handlerOut);.
If you need to, then use on for mouseenter and mouseleave events:
$(document).on('mouseenter', '.top-level', function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}).on('mouseleave', '.top-level', function (event) {
$(this).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
Try
$('.top-level').hover(function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}, function(){
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
Alright have been working to switch to jQuery 1.7's new and improved .on() function instead of relying on .live().
Here's what get's me, when you've got multiple bindings, .live() was great letting you do them all in a simple function. For example:
$('.my_thing').live({
mouseover: function(e) {
console.log('hey imma moused over');
},
mouseout: function(e) {
console.log('hey imma moused out');
}
});
Very simple! How would you do this with .on()? Here's as close as I can get and it still feels messy.
$(document).on('mouseover','.my_thing', function(e) {
console.log('hey imma moused over');
}).on('mouseout','.my_thing', function(e) {
console.log('hey imma moused out');
});
Feels messy, no? There must be a better way.
PS - This has got to be one of the worst functions EVER if you're trying to learn more about it on Google.
$(document).on({
mouseover: function(e) {
console.log('hey imma moused over');
},
mouseout: function(e) {
console.log('hey imma moused out');
}
}, '.my_thing' );
Just use an events map in addition to a selector:
$(document).on({
mouseover: function(e) {
console.log('hey imma moused over');
},
mouseout: function(e) {
console.log('hey imma moused out');
}
}, '.my_thing');
Here's a demo.