Solved

JS error

Posted on 2016-09-16
10
39 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
  • 5
  • 4
10 Comments
 
LVL 6

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 6

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
 

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 6

Expert Comment

by:Mukesh Yadav
ID: 41801913
Please tell us what you are trying to do?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 6

Accepted Solution

by:
Mukesh Yadav earned 500 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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now