Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jQuery find empty inputs for particular form

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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(...);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading