jQuery class selector oddness
Posted
by x3sphere
on Stack Overflow
See other posts from Stack Overflow
or by x3sphere
Published on 2010-05-09T09:38:18Z
Indexed on
2010/05/09
9:48 UTC
Read the original article
Hit count: 205
I'm using jQuery to change the background image of a button depending on the class associated with it on hover. It only works if I put the hover statements in separate functions, however. Why is this?
Here's the NON working code, always evaluates to the .submit hover statement, even when that class is removed via the keyup event.
$(function() {
{
$('.submit-getinfo').hover(function ()
{
$(this).css( {backgroundPosition: "right bottom"} );
}, function() {
$(this).css( {backgroundPosition: "right top"} );
//$(this).removeClass('submithover');
});
$('.submit').hover(function ()
{
$(this).css( {backgroundPosition: "left bottom"} );
}, function() {
$(this).css( {backgroundPosition: "left top"} );
//$(this).removeClass('submithover');
});
}});
Working code:
$(function() {
{
$('.submit').hover(function ()
{
$(this).css( {backgroundPosition: "left bottom"} );
}, function() {
$(this).css( {backgroundPosition: "left top"} );
//$(this).removeClass('submithover');
});
}});
$('#test').bind('keyup', function() {
if (url == 'devel') {
$("#submit").addClass("submit-getinfo").removeClass("submit");
$('.submit-getinfo').hover(function ()
{
$(this).css( {backgroundPosition: "right bottom"} );
}, function() {
$(this).css( {backgroundPosition: "right top"} );
//$(this).removeClass('submithover');
});
} } );
I just fail to see why I have to put the hover statements in separate functions, instead of sticking both in the main DOM.
© Stack Overflow or respective owner