Forum Moderators: open

Message Too Old, No Replies

A weird JS error :-)

I'm going crazy

         

ShepMode

3:18 pm on Oct 22, 2003 (gmt 0)

10+ Year Member



Ok to understand what I'm doing you'll have to load the HTML file below.

When you select an item on the left and select the ">>" button, some alerts will come up telling you what's happening. The first 2 alerts will tell you which items are being added to the array arrayFbox. arrayFbox is a multidimensional array. The values being added are the 2 items that you did not select from the left box.

Next, you will get an alert telling you that 2 items are in arrayFbox (arrayFbox.length).

Finally, two more alerts will come up as the script loops through arrayFbox to check it. Here is where the error is first seen! If you select People or Cartoon both values in the array will be array(5,Garden) or if you select Garden both values will be array(4,cartoon).

The values for the first two alerts and the last two alerts should be the same, but they aren't. Please help.

Please tell me why this is happening! I'm pulling my hair out!

Thanks,

Keiron

The code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Please Work!</title>
</head>

<body>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--
// fbox = option moved from, tbox = option moved to.
function move (fbox,tbox) {
var arrayFbox = new Array();
var arrayFboxInside = new Array();
var arrayTbox = new Array();
var arrayTboxInside = new Array();
var i;
var j;
var x;
j = 0;
x = 0;
// Create the original fbox list in an array, minus the value moved.
for (i = 0; i < fbox.options.length; i++) {
// The items moved.
if (fbox.options[i].selected) {
arrayTboxInside[0] = fbox.options[i].value;
arrayTboxInside= fbox.options[i].text;
if ((tbox.options.length == 1) && (tbox.options[0].value == "")) {
arrayTbox[0] = arrayTboxInside;
}
else {
arrayTbox[tbox.options.length+x] = arrayTboxInside;
}
x++;
arrayTboxInside.splice(0);
}
// The items not moved.
else {
arrayFboxInside[0] = fbox.options[i].value;
arrayFboxInside[1] = fbox.options[i].text;
arrayFbox[j] = arrayFboxInside;
arrayFboxInside.splice(0);
alert("arrayFbox["+j+"] values as I add them to arrayFbox: "+arrayFbox[j][0]+", "+arrayFbox[j][1]);
j++;
}
}
alert("arrayFbox Length: "+arrayFbox.length);
for (i=0; i < arrayFbox.length; i++) {
alert("arrayFbox["+i+"] values after I have finished adding them to arrayFbox: "+arrayFbox[i][0]+", "+arrayFbox[i][1]);
}
}
// -->
</script>

<form name="form" method="post" action="customer-content.php?add">
<table cellspacing="0" cellpadding="5">
<tr>
<td class="main_body"><select name="list1" multiple style="width:150; height: 150;" onchange="document.form.list2.selectedIndex='-1';">
<option value="3">People</option>
<option value="4">Cartoon</option>
<option value="5">Garden</option>
</select>
</td>
<td align="center"><input type="button" value=" &gt;&gt; " onClick="move(this.form.list1,this.form.list2)"><br>
<input type="button" value=" &lt;&lt; " onClick="move(this.form.list2,this.form.list1)"></td>
<td><select name="list2" multiple style="width:150; height: 150;" onchange="list_content.innerHTML='<img src=images/customer_images/thumb-'+imageDetails[this.form.list1.selectedIndex]+' border=1><br>'+imageTitles[this.form.list2.selectedIndex]; document.form.list1.selectedIndex='-1';"></select></td>
</tr>
</table>
</form>

</body>
</html>

[1][edited by: korkus2000 at 3:46 pm (utc) on Oct. 22, 2003]
[edit reason] fixed spelling ;) [/edit]

HocusPocus

7:32 pm on Oct 22, 2003 (gmt 0)

10+ Year Member



I couldn't get the alerts to show at first

try

arrayFbox = new Array();
arrayFboxInside = new Array();
arrayTbox = new Array();
arrayTboxInside = new Array();

for starters. also

if (fbox.options[i].selected) {
arrayTboxInside[0] = fbox.options[i].value;
arrayTboxInside[1]?= fbox.options[i].text;
if ((tbox.options.length == 1) && (tbox.options[0].value == "")) {
arrayTbox[0] = arrayTboxInside[1]? ;

Lost the plot after that.

broniusm

12:26 am on Oct 23, 2003 (gmt 0)

10+ Year Member



Keiron-

My guess is you're in IE. Me, too, and I get an error.

Two things right off the bat:
1) get yourself the free Microsoft Script Debugger [microsoft.com] appropriate for your O/S.
2) run your code and when prompted to debug, click YES. This will shoot you right into the code where the bug occurs.

As for the error, it must be the splice method [crockford.com] which, according to this link, is not supported in IE 5.

Finally, if you're just wanting to shoot values over to the other side, I imagine there must be an easier way. You've got the structure for it already.

Don't pull out your hair... although it does explain the stereotype.