473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

A very weird JS error :-)

Ok to understand what I'm doing you'll have to load the HTML file attached
(code also 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).

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>Fucking 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[1] = 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)"><b r>
<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.selectedInde
x]+' border=1><br>'+imageTitles[this.form.list2.selectedIndex];
document.form.list1.selectedIndex='-1';"></select></td>
</tr>
</table>
</form>

</body>
</html>


Jul 20 '05 #1
1 1557
Oz
Oops you're making a simple mistake

your code reads:

arrayFboxInside[0] = fbox.options[i].value;
arrayFboxInside[1] = fbox.options[i].text;
arrayFbox[j] = arrayFboxInside;
arrayFboxInside.splice(0);

The problem is that you keep setting the arrayFbox[j] values to refence the same object (arrayFboxInside), and you keep changing the same params of thay array (0 and 1). Did you notice that the value that keeps poping up is the last value in the option box (5, Garden)? This is becuase it is the last value that was written into arrayFboxInside[0] and arrayFboxInside[1].

The simple fix is to create new arrayFboxInside objects each time you loop so your else statement will look like:

var arrayFboxInside = new Array();
arrayFboxInside[0] = fbox.options[i].value;
arrayFboxInside[1] = fbox.options[i].text;
arrayFbox[j] = arrayFboxInside;

or a simplier syntax would look like

arrayFbox[j] = [fbox.options[i].value, fbox.options[i].text];

Note: you have the same problem in your if statement

"Keiron Waites" <we*******@NOSPAMsharemonkey.com> wrote in message news:bn**********@sparta.btinternet.com...
Ok to understand what I'm doing you'll have to load the HTML file attached
(code also 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).

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>Fucking 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[1] = 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)"><b r>
<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.selectedInde
x]+' border=1><br>'+imageTitles[this.form.list2.selectedIndex];
document.form.list1.selectedIndex='-1';"></select></td>
</tr>
</table>
</form>

</body>
</html>

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed...
5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
1
by: amit | last post by:
I am trying to compile the sample program genwin.sqc, using nsqlprep which is used to precompile embedded sql in C. I am getting weird errors and that is because windows.h is included in the...
2
by: Kathy Houtami | last post by:
Hi there I've been encountered with weird compile error using Access 97. The error message is "Member or data member is not found" and it highlighted a line of code that has not be changed and...
6
by: gh0st54 | last post by:
Hi I have a weird javascript error that only happens on my live server when i open the page http://www.imrated.co.uk/reg.aspx i get the error 'Problems with this page ... blablabla Line : 3...
0
by: Alan Silver | last post by:
Hello, I have two weird problems here. I have a master page file that works absolutely fine. When I load it up in VWD, I get a couple of weird (to me) errors. First, I get the error...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
7
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created...
6
by: =?Utf-8?B?amVmZmVyeQ==?= | last post by:
i need help with a combo box and this same code works on my first tab with a combo box. The error or problem i have is this code causes an index out of range error when i run it on my second combo...
4
by: d3vkit | last post by:
I recently had a friend pass along some web work for me; a simple update. I for some reason decided I would do some overhauling of the page (he had been doing straight html and I figured some php...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.