?
Solved

problem with for loop

Posted on 2013-10-23
3
Medium Priority
?
171 Views
Last Modified: 2013-10-23
The code below works, but only if I include an alert.  When i remove the alert, it does not pass the array values through as it should

			var theoption = ["first","second"];
			for (var i=0;i<theoption.length;i++)
				{			
				$.getJSON("testsample?menu="+theoption[i]+"",function(results){
								var options="";
								
								for(resd in results.data)					
								{
									options+="<option value='"+results.data[resd].id+"' >"+results.data[resd].Title+"</option>";
								} 
								$("#"+theoption[i]+"test").html(options);
				});		
				alert(theoption[i])	
		}

Open in new window

0
Comment
Question by:debbieau1
[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
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 2000 total points
ID: 39593720
Try it like this:
var theoption = ["first", "second"];
for (var i = 0; i < theoption.length; i++) {
    getValues(theoption[i]);
}

function getValues(option){
    $.getJSON("testsample?menu=" + option + "", function (results) {
        var options = "";

        for (resd in results.data) {
            options += "<option value='" + results.data[resd].id + "' >" + results.data[resd].Title + "</option>";
        }
        $("#" + option + "test").html(options);
    });
}

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39593728
You're having this problem because you're using the loop index inside the ajax callback.
As the callback is asyncronous, when it actually arrives the value of i is no longer the one at request time giving you unexpected results.

The solution above sends the current 'theoption' to the processing function, decoupling the callback process from the loop itself.
0
 
LVL 1

Author Closing Comment

by:debbieau1
ID: 39595867
Thanks a lot.  Great clear explanation
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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

764 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