views:

23

answers:

1

I have a a handful of dynamically generated inputs. Some have IDs, some do not.I also have an array of IDs. I need to loop through the array of IDs and input the next available ID into the next input without a value. I have tried

$.each(event_array, function(intIndex, objValue){
  $('.event-data').find('.event-id').each(function(i){
    $(".event-id:empty").val(objValue);
  });
});

And

$('.event-data').find('.event-id').each(function(i){
  $(".event-id").val(event_array[i]);
});

Obviously the second one doesn't search for the value of the input and the first one uses :empty which I have found out is not for what I am using it for.

Any idea what I need to use for this to work? Thanks!!

+2  A: 

This will filter through the .event-id elements for the ones that don't have a value, then passes a function to .val() that returns the array element matching the current index.

$('.event-data .event-id').filter(function() {
    return this.value === '';  // filter items that don't have a value
})
.val(function(i,val) { return event_array[i]; }); // Call .val() on the resulting
                                                  //   set, using the i value of
                                                  //   each to get the next item
                                                  //   from your array

Passing a function to .val() requires jQuery 1.4 or later.

patrick dw
This worked great. Thank you so much!
LostInQuery