expanding/collapsing div using jQuery

Posted by Hristo on Stack Overflow See other posts from Stack Overflow or by Hristo
Published on 2010-06-09T03:57:45Z Indexed on 2010/06/09 4:02 UTC
Read the original article Hit count: 172

Filed under:
|
|
|
|

I'm trying to expand and collapse a <div> by clicking some text inside the <div>. The behavior right now is very odd. For example, if I click the text after the <div> is expanded... the <div> will collapse and then expand again. Also, if I click somewhere inside the div after it is expanded, it will collapse again, and I think that is because I'm triggering the animation since the <div> being animated is inside the wrapper <div>. Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>

    <!-- Links -->
    <link rel="stylesheet" type="text/css" href="style.css" />

    <!-- Scripts -->
    <script type="text/javascript" src="jQuery.js"></script>
    <script>

        // document script
        $(function(){

            // login box event handler
            $('#login').click(function() {
                $('#loginBox').toggle(
                    function() {
                        $('.loginBox').animate({ 
                            height: '150px'
                        }, 
                            '1000'
                        );
                        $('#username').show();
                        $('#password').hide();
                        $('#placeHolder').show();                   
                    }, 
                    function() {
                        $('.loginBox').animate({ 
                            height: '50px'
                        }, 
                            '1000'
                        );
                        $('#username').hide();
                        $('#password').hide();
                        $('#placeHolder').hide();                                       
                    }
                );
            });


            // username field focus and blur event handlers
            $('#username').focus(function() {
                if($(this).hasClass('placeHolder')){
                    $(this).val('');
                    $(this).removeClass('placeHolder');
                }
            });
            $('#username').blur(function() {
                if($(this).val() == '') {
                    $(this).val('Username');
                    $(this).addClass('placeHolder');
                }
            });         

            // password field focus and blur event handlers
            $('#placeHolder').focus(function() {
                $(this).hide();
                $('#password').show();
                $('#password').focus();
                $('#password').removeClass('placeHolder');
            });
            $('#password').blur(function() {
                if($(this).val() == '') {
                    $('#placeHolder').show();
                    $(this).hide();
                }   
            });

        });

    </script>

</head>
<body>

    <div id="loginBox" class="loginBox">
        <a id="login" class="login">Proceed to Login</a><br />
        <div>
            <form>
                <input type="text" id="username" class="placeHolder" value="Username" />
                <input type="password" id="password" class="placeHolder" value="" />
                <input type="text" id="placeHolder" class="placeHolder" value="Password" />
            </form>
        </div>
    </div>

</body>
</html>

Any ideas?

Thanks, Hristo

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about animation