What is wrong in that javascript/jquery array iteration -
i trying declare array, push colection of arrays , iterate colection
my js:
var colection = []; var array = ["a1", "a2"]; colection.push(array); array = ["b1", "b2"]; colection.push(array); $.each(colection, function(array){ var p = "<p>1:"+array[0] + "<br>2:"+array[1]+"</p>"; $("#test").append(p); }); my html:
<div id="test"> </div> i want result:
1:a1 2:a2 1:b1 2:b2 but return only:
1:undefined 2:undefined 1:undefined 2:undefined
the first argument of function in each syntax index of item change script like:
var colection = []; var array = ["a1", "a2"]; colection.push(array); array = ["b1", "b2"]; colection.push(array); $.each(colection, function(index, array){ var p = "<p>1:"+array[0] + "<br>2:"+array[1]+"</p>"; $("#test").append(p); }); jsfiddle: http://jsfiddle.net/9kvnm/
edit: ref: api.jquery.com/jquery.each
thx kevin
Comments
Post a Comment