Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

JS error

Hi,

When I run the following code, I received the error

test.aspx:15 Uncaught SyntaxError: Unexpected token }


Any ideas?  thanks

    function AddListElements(frm, list) {
        if (list.length != 0) {
            var eArray = new Array();
            var tArray = new Array();
            //looks for all the selects on the left side that contains selected items
            for (var j = list.length - 1; j >= 0; j--) {
                value = list.options[j].value;
                text = list.options[j].text;
                eArray[countElements] = value;
                tArray[countElements] = text;
                if (!isInArray(value, lArray)) {
                    lArray[countElements][0] = list.options[j].value;
                    lArray[countElements][1] = list.name;
                    lArray[countElements][2] = 1;
                    lArray[countElements][3] = list.options[j].text;
                }
                RemoveOption(list, j);
                DecrementIndexes(list.name);
                countElements++;
            }
            for (var j = eArray.length - 1; j >= 0; j--) {
                if (eArray[j] != null) {
                    AddOption(frm.mainlist, eArray[j], tArray[j]);
                    eArray[j] = null;
                    tArray[j] = null;
                    IncrementIndexes("mainlist");
                }
            }
        }
    }

Open in new window

Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

There is not such error in this code?

Share complete code or share url of page to check.
Avatar of mcrmg
mcrmg

ASKER

this is inside a form

			<SELECT multiple size="7" name="testCategory" > 
			      <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
			</SELECT> 
			    			<INPUT onclick="AddListElements(this.form,"testCategory")" type=button value=">>">

Open in new window


thanks
Here problem is " in onclick replace it by ':

<INPUT onclick="AddListElements(this.form, 'testCategory')" type=button value=">>11">
Avatar of mcrmg

ASKER

Thank you very much

When I show alert(list.length);, it shows 12, why does it show 12? I thought there are only 4?

then the error shows

Uncaught TypeError: Cannot read property '11' of undefined


I am sorry if the answer is too obvious.  thanks
Please tell us what you are trying to do?
Avatar of mcrmg

ASKER

sorry about that. here is the code. When I select items from the left and press ">>", the slected items should go to the right side.  

I am getting "Uncaught TypeError: Cannot read property '11' of undefined"


thanks

<form name="form1">

<SELECT multiple size="7" name="testCategory" > 
			      <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
			</SELECT> 
			    			<INPUT onclick="AddListElements(this.form,'testCategory')" type=button value=">>">
			    			
			    			
<SELECT  CLASS="AdHoc_Form_RIGHT" multiple size=17 name=mainlist></SELECT> 			    			
			    			</form>
			    			
			    			
<script>

    function AddListElements(frm, list) {
        if (list.length != 0) {
            var eArray = new Array();
            var tArray = new Array();
            //looks for all the selects on the left side that contains selected items
            for (var j = list.length - 1; j >= 0; j--) {
                value = list.options[j].value;
                text = list.options[j].text;
                eArray[countElements] = value;
                tArray[countElements] = text;
                if (!isInArray(value, lArray)) {
                    lArray[countElements][0] = list.options[j].value;
                    lArray[countElements][1] = list.name;
                    lArray[countElements][2] = 1;
                    lArray[countElements][3] = list.options[j].text;
                }
                RemoveOption(list, j);
                DecrementIndexes(list.name);
                countElements++;
            }
            for (var j = eArray.length - 1; j >= 0; j--) {
                if (eArray[j] != null) {
                    AddOption(frm.mainlist, eArray[j], tArray[j]);
                    eArray[j] = null;
                    tArray[j] = null;
                    IncrementIndexes("mainlist");
                }
            }
        }
    }
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

Thank you
list is a string - you are passing the string 'testCategory' to the function - not the actual control.

Therefore, when you do list.length it is giving you the length of the string 'testCategory' - not the length of the list.
Avatar of mcrmg

ASKER

I see.  Thanks for the direction.