George Sisco George Sisco - 5 months ago 13
jQuery Question

how to apply each index value of an array to each value of a group of selected elements

So if I have an array of 2 values and I want to apply those values to two elements, I can't seem to figure out a way to loop through the selected elements and have a counter or something to represent the index of the array value I want to apply.

Anyone?

For example:

myTotal = new Array("7.00", "10.00");

<input class="theTotalInput"></input>
<input class="theTotalInput"></input>


So, once I select the inputs,

$(".theTotalInput")


...

yeah... this is where Im stuck. I cannot find any example documentation that shows applying arrays of values to a bunch of elements in turn - all I have seen is applying the same values to a bunch.

Thanks for any help.

Answer

var i = 0;
$(".theTotalInput").each(function() {
    $(this).val(myTotal[i++])
});