views:

13

answers:

2

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.

A: 
var i=0;
$(".theTotalInput").each(function(){
$(this).val(myTotal[i++])
});
Funky Dude
Thanks. I ended up using this: while I was waiting:http://docs.jquery.com/Core/eq#positionWhich is weird, because I guess I was trying to do it wrong before using this:http://docs.jquery.com/Selectors/eq#indexSo mine ended up looking like: for(i=0;i<myTotal.length;i++){$(".theTotalInput").eq(i).val(myTotal[i]);}I'm book marking yours, too, though. Thanks!
George Sisco
A: 

Another, possibly shorter way:

$(["7.00","10.00"]).each(function(i, val) {
    $(".theTotalInput").eq(i).val(val);
});

or:

$(".theTotalInput").each(function(i, el){
    $(el).val(myTotal[i]);
});
David