jQuery dynamic field classes not being assigned as desired.

Posted by Simon on Stack Overflow See other posts from Stack Overflow or by Simon
Published on 2010-04-15T22:46:53Z Indexed on 2010/04/15 22:53 UTC
Read the original article Hit count: 408

Filed under:
|

Hello,

I'm working on a fancy login page with 4 unique states attributed through classes for each field (normal, focus, active-on, active-off).

Normal is the default style. Focus is the focus style when nothing is typed. Active-on is the focus style when something has been typed. Active-off is for field that have user text in them, but are not focused right now.

Here's a demo to help you understand what I'm doing: http://www.controlstack.com/login

My JS is working almost correctly (thanks to some folks on this site), except in 2 cases:

  1. If I enter something in the username field, then tab over to the password field, it does not add the ".focus" class to the password field.

  2. If I blur out of the username field, then focus back on it, enter a few characters, then delete them, it does not add the ".focus" field.

Here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">

<head>
<title>Login</title>
<style>
    input.field {height: 39px; width: 194px; background: url(login-fields.png) no-repeat; overflow: hidden; border: none; outline: none; float: left; margin-right: 7px;}
    input.field#username {padding: 0 12px;}
    input.field#username.focus {background-position: 0 -39px;}
    input.field#username.active-on {background-position: 0 -78px;}
    input.field#username.active-off {background-position: 0 -117px;}    
    input.field#password {background-position: -218px 0; padding: 0 12px;}
    input.field#password.focus {background-position: -218px -39px;}
    input.field#password.active-on {background-position: -218px -78px;}
    input.field#password.active-off {background-position: -218px -117px;}
    input.field#go {background-position: -436px 0; width: 88px; margin: 0; cursor: pointer;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    loginField = $('.field');
    firstField = $('.field.first');

    firstField.focus();

    loginField.focus(function(){
        loginVal1 = loginField.val();
        if (!loginVal1){
            $(this).removeClass('active-off').addClass('focus');        
        }
        else {
            $(this).removeClass('active-off').addClass('active-on');
        }
    });

    loginField.live('keydown', function(){
        $(this).addClass('active-on').removeClass('active-off');
    }).live('keyup', function(){
        $(this).toggleClass('active-on', $(this).val() != '');
    })

    loginField.blur(function(){
        loginVal2 = loginField.val();
        if (!loginVal2){
            $(this).removeClass('focus').removeClass('active-on');
            $(this).val('');
        }
        else {
            $(this).removeClass('focus').removeClass('active-on').addClass('active-off');
        }
    }); 

}); 
</script>
</head>

<body>
<h1>Login to your account</h1>
<form method="post" action="/">
    <fieldset>
        <input type="text" class="field first focus" id="username" />
        <input type="text" class="field" id="password" />
        <input type="submit" value="" class="field" id="go" alt="login" title="login" />
    </fieldset>
</form>
</body>

</html>

Your help is much appreciated!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about webforms