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!!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.