...

View Full Version : Required naming syntax breaks functions



Buzzle
06-23-2009, 12:12 AM
Hey all,

I have 2 select windows and have a function to move items from one select window to another. That was working fine until I had to change the names of each box in order to be able to post the select window as an array in PHP.


<select size="10" name="select_list1[]" id="select_list1[]" MULTIPLE>


<select size="10" name="select_list2[]" id="select_list2[]" MULTIPLE>

This is the function call:

<input type="button" value=" --> " onClick="SelectMoveRows(document.form.select_list1, document.form.select_list2);"/>

And this is the function that moves the options from one window to the other:


function SelectMoveRows(SS1,SS2) {
var SelID='';
var SelText='';

// Move rows from SS1 to SS2 from bottom to top
for (i=SS1.options.length - 1; i>=0; i--) {
if (SS1.options[i].selected == true) {
SelID=SS1.options[i].value;
SelText=SS1.options[i].text;
var newRow = new Option(SelText,SelID);
SS2.options[SS2.length]=newRow;
SS1.options[i]=null;
}
}
}



The errors I am getting now are:

Error: SS1 is undefined

and if I try to change the function call (adding []'s), I get a syntax error because of it.

Error: syntax error
Source Code:
SelectMoveRows(document.form.select_list1[], document.form.select_list2[]);

How can I change the functions so that it accepts the square brackets? They are necessary unless you know of a better way to post a select form in PHP.

Thanks for any information.

randomuser773
06-23-2009, 12:57 AM
Try:

onClick = "SelectMoveRows( document.form[ 'select_list1[]' ], document.form[ 'select_list2[]' ] );"

tomws
06-23-2009, 02:11 AM
I don't think the square brackets are required on the id attribute - just the name. After a glance at google, some posts suggest that square brackets on the ID may cause inconsistent behavior.

Buzzle
06-23-2009, 04:04 AM
Thanks randomuser, that worked. Javascript syntax always seems to surprise me. I never really sat down to thoroughly investigate it.

@tomws, Thanks for the advice. I don't normally do that, I was just frustrated and trying everything to get it to work. I'll likely take it out since I am not really even using the id for anything.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum