views:

60

answers:

1

example of what I'm trying to do

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"&gt;
<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

+1  A: 

Yeah, use event.stopPropagation();

  $('#login').click(function(event) {
         event.stopPropagation();

that way, the event doesn't hit #loginBox. Also, if you're worried about animations getting stuck in the queue (clicking too many times and the animation going and going) you can use stop..

$('#username').stop().show();

further on toggle - your implemation adds a click handler to #loginBox on the click of login.. toggle is a click handler..

// login box event handler

 $('#login').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();                                       
            }
        );

is a bit more accurate..

Dan Heberden
It doesn't seem to work for me. I just tried the event.stopPropagation() and the box never expands when I click the text.
Hristo
Well, further inspection: you are calling toggle on the loginBox - that is a click handler, but you want the click of login to fire..
Dan Heberden
Correct. So... I didn't know toggle was a click handler. How would I fix this?
Hristo
By adding toggle to the element you want to alternate clicks for (#login).. or just do a regular click and check for the height of the div...
Dan Heberden
ahhh :) Thank you! I just didn't know toggle was a click handler... I started messing with jQuery... today :)
Hristo