Home > OS >  jQuery find empty inputs for particular form
jQuery find empty inputs for particular form

Time:12-13

I am trying to find and set values on empty form fields, but I need to only select the fields that are in a particular form. The form does not have an ID, but I know which form I am in by the link that was clicked. Current code is like this:

$('.analysis__main').on('click', 'a[data-allocation]', function (e) {
       e.preventDefault();
       var $link = $(e.currentTarget);
       form = $link.parents('form')
       var emptyFields = $('input:text').filter(function() { return $(this).val() === ""; });
       emptyFields.each(function() {
           $(this).val('50.0')
       });
 });

The problem is that the current code assigns values for ALL empty fields on the page. I cannot figure out a way to assign values only for the inputs on the selected form.

CodePudding user response:

You need to look inside the form instance using find() rather than use the generic selector for all text inputs in the page

Change

var emptyFields = $('input:text').filter(...);

To:

var emptyFields = form.find('input:text').filter(...);
  • Related