Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Dickens
18,352 Points.on() Can we use other event shorthand or are we stuck with 'click'?
...from http://api.jquery.com/
""Any event names can be used for the events argument. jQuery will pass through the browser's standard JavaScript event types, calling the handler function when the browser generates events due to user actions such as click"""
But when I use 'hover' or 'toggle' nothing happens, What am I doing wrong
$('#circle1').on('hover', function(){
$(this).addClass('new-class2');
})
3 Answers
Ken jones
5,394 PointsSupport for 'hover' within jQuery's .on() method was deprecated in 1.8 and removed in 1.9.
I think you have to use mouseenter and mouseleave now.
Or just use the .hover(); method.
Jonathan Grieve
Treehouse Moderator 91,254 PointsIf I remember correctly with the hover event you need to provide 2 callback functions as arguments. The state change when you hover over the element and the state change when you hover out.
Here's the docs for the hover() method. https://api.jquery.com/hover/ :-)
Andrew Dickens
18,352 PointsThanks Jonathan,
sure i can get it to work this way
$( '.square' ).hover(
function() {
$( this ).addClass( 'new-class2' );
}, function(){
$( this ).removeClass( 'new-class2' );
}
);
But when I use .on() it's still not working, can't see the syntax to separate the two callback functions properly
$('.circle').on('hover', function() {
$( this ).addClass( 'new-class2' );
}, function(){
$( this ).removeClass( 'new-class2' );
}
);