javascript - Possible to add a loop while appending to the DOM? -
i'm appending information dom, realized of information need nested within object. when below bit of code, part within loop returns undefined. there way iterate on , pull out information can append page:
function placeonpage(allstopswithroutes){ allstopswithroutes.foreach(function(stop){ $('#stops').append("stop: " + stop.crossstreets + "<br>" + // loop here stop.routes.foreach(function(route){ "bus name: " + busname + "<br>" + "stops away: " + stopsaway + "<br>" + "destination: " + destination + "<p>" }); // end loop ); }); }
i don't think strings can concatenated way. change approach concatenate before hand , store in variable. append variable
function placeonpage(allstopswithroutes) { allstopswithroutes.foreach(function(stop) { var concatenatedstr = ''; stop.routes.foreach(function(route) { concatenatedstr += ("bus name: " + busname + "<br>" + "stops away: " + stopsaway + "<br>" + "destination: " + destination + "<p>"); }); $('#stops').append("stop: " + stop.crossstreets + "<br>" + concatenatedstr); }); }
Comments
Post a Comment