views:

16

answers:

1

I have a form that a user can click a button to add another set of inputs. So I am using eName[] for example. I was hoping to use jquery to do this, and thought live() would be a great way to loop through all the elements the user created to see if they are blank but I am not sure if I can do this without binding to an event. So I am thinking something like:

$(form).submit(funciton()({
  //other validation here
  $(".eName").live(bindToWhat?, function() {
    //loop and check val();
  });
});

Or maybe this is the wrong approach.

I hope this is enough info, if not lemme know. Thanks!!

+1  A: 

Don't bind to inputs, do this instead:

$(form).submit(funciton()({
  $(".eName").each(function() {
    if ($(this).val() === ''){
      alert('This field can not be empty');
      $(this).focus();
    }
  });
});

Where eName is supposed to be the class assigned to the fields.

Sarfraz
each() won't pick up the new elements the user created will it? I thought each() was only aware of the elements that were created onload.
lardlad