I have two arrays: one is full of lines, the other is an array of objects. The indices for each correspond, and I want to replace the text of each of the objects in my array of objects with the corresponding text in my array of strings.
For example, I have an array like this:
var textarr = ["value1", "value2", "value3"]
and an array of jQuery objects containing a bunch of span elements:
var spans = $("span.myClass"); var spanarr = $.makeArray(spans);
I am trying to use $ .each () to iterate over each span and use the corresponding index of my text array to assign a text value to the current interval.
I tried a couple of different ways and nothing works. I am missing some kind of logic here, but why doesn't this work ?:
i = 0; jQuery.each(spanarr, function() { $(this).text(textarr[i]); i++; });
EDIT: I think maybe the rest of my function may cause this to not work. Here's the whole script:
$("span input:radio").click(function() { if (($(this).is(":checked")) == true) { var parent = $(this).parent(); var aunts = parent.parent().children(); var parentIndex = aunts.index(parent); var indexToNthChild = parentIndex + 1; var otherSpans = $(".DropDownMenu span:nth-child(" + indexToNthChild + ")"); var position = parent.position(); var topValue = position.top; var smallPrice = otherSpans.children("span.dropDownPrice"); var pricearr = jQuery.makeArray(smallPrice); var textarr = []; jQuery.each(pricearr, function() { textarr[i] = $(this).text(); }); alert(textarr);
source share