...

View Full Version : For-Loop Breaks Code. Why?



sushi4664
07-07-2011, 05:43 AM
I have the following 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?

devnull69
07-07-2011, 07:17 AM
You got the array indexes wrong for the second array. I think this is what you really wanted to do


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();
});
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum