?
Solved

JS error

Posted on 2016-09-16
10
Medium Priority
?
70 Views
Last Modified: 2016-09-17
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

0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41801863
There is not such error in this code?

Share complete code or share url of page to check.
0
 

Author Comment

by:mcrmg
ID: 41801877
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
0
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41801893
Here problem is " in onclick replace it by ':

<INPUT onclick="AddListElements(this.form, 'testCategory')" type=button value=">>11">
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:mcrmg
ID: 41801904
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
0
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41801913
Please tell us what you are trying to do?
0
 

Author Comment

by:mcrmg
ID: 41801933
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

0
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 2000 total points
ID: 41801998
I think you should try this plugin instead of developing it from scratch

http://loudev.com/#demos
0
 

Author Closing Comment

by:mcrmg
ID: 41802147
Thank you
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41802713
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.
0
 

Author Comment

by:mcrmg
ID: 41802958
I see.  Thanks for the direction.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question