Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For-Loop Breaks Code. Why?

    I have the following code:

    Code:
    <script>
            $(function() {
                
                var inputArr = new Array("#register_id #username_id",
                                        "#register_id #email_id",
                                        "#register_id #password1_id",
                                        "#register_id #password2_id",
    	                            "#signin_id #email_id",
    	                            "#signin_id #password_id");
                                        
                var inputToOverlayDict = new Array();
                
                inputToOverlayDict[inputArr[0]] = "#register_id #usernameOverlay_id";
                inputToOverlayDict[inputArr[1]] = "#register_id #emailOverlay_id";
                inputToOverlayDict[inputArr[2]] = "#register_id #passwordOverlay1_id";
                inputToOverlayDict[inputArr[3]] = "#register_id #passwordOverlay2_id";
    	    inputToOverlayDict[inputArr[4]] = "#signin_id #emailOverlay_id";
    	    inputToOverlayDict[inputArr[5]] = "#signin_id #passwordOverlay_id";
    
    	var i;
    for(i=0; i<6; i++) {
    	$(inputArr[i]).focus(function() {
                    $(inputToOverlayDict[inputArr[i]]).hide();
                });
    }
                
                /*
                $(inputArr[0]).focus(function() {
                    $(inputToOverlayDict[inputArr[0]]).hide();
                });
    	    $(inputArr[0]).blur(function() {
    	        if($(this).val().length == 0)
    	            $(inputToOverlayDict[inputArr[0]]).show();
    	    });
                
                $(inputArr[1]).focus(function() {
                    $(inputToOverlayDict[inputArr[1]]).hide();
                });
                
                $(inputArr[2]).focus(function() {
                    $(inputToOverlayDict[inputArr[2]]).hide();
                });
                
                $(inputArr[3]).focus(function() {
                    $(inputToOverlayDict[inputArr[3]]).hide();
                });
    
    	    $(inputArr[4]).focus(function() {
                    $(inputToOverlayDict[inputArr[4]]).hide();
                });
    
    	    $(inputArr[5]).focus(function() {
                    $(inputToOverlayDict[inputArr[5]]).hide();
                });
    
            */
            });
        </script>
    The whole idea is that if a certain div object comes to focus, something else disappears (using JQuery). For some reason the code in the comments is working fine, but the for loop breaks everything down. Why?
    -sushi
    -visit http://napkinz.com/index.php - funny comic updated weekly

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You got the array indexes wrong for the second array. I think this is what you really wanted to do
    Code:
                var inputArr = new Array("#register_id #username_id",
                                        "#register_id #email_id",
                                        "#register_id #password1_id",
                                        "#register_id #password2_id",
    	                            "#signin_id #email_id",
    	                            "#signin_id #password_id");
                                        
                var inputToOverlayDict = new Array();
                
                inputToOverlayDict[0] = "#register_id #usernameOverlay_id";
                inputToOverlayDict[1] = "#register_id #emailOverlay_id";
                inputToOverlayDict[2] = "#register_id #passwordOverlay1_id";
                inputToOverlayDict[3] = "#register_id #passwordOverlay2_id";
    	    inputToOverlayDict[4] = "#signin_id #emailOverlay_id";
    	    inputToOverlayDict[5] = "#signin_id #passwordOverlay_id";
    
    	var i;
    for(i=0; i<6; i++) {
    	$(inputArr[i]).focus(function() {
                    $(inputToOverlayDict[i]).hide();
                });
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •