Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
Javascript FormData object Ignoring some fields (under certain circumstances)

I have a dynamic form that is building and rebuilding categories based on the users input. Basically it is a product finder that narrows down the results as more categories are checked. I am mostly doing this with jQuery, but I found this piece of code that takes the form data and turns it into JSON.

var fData = new FormData(document.querySelector('#prodFilter'))
// Build Filters
buildFilterData(fData);


function buildFilterData(e) {
    const object = {};
    e.forEach((value, key) => {
        // Reflect.has in favor of: object.hasOwnProperty(key)
        if(!Reflect.has(object, key)){
            object[key] = value;
            return;
        }
        if(!Array.isArray(object[key])){
            obFOrject[key] = [object[key]];    
        }
        object[key].push(value);
    });

    // Stringify Result
    let formResult = JSON.stringify(object);
    params = formResult;
    return params;
}

This little snippet is cool because it can handle multiple checked values. I get a result like:

{"keyword":"","product_type":"Category","product_category":["Sub-Cat1","Sub-Cat2"]}

This is/was working great until I tried to add a keyword search box to further narrow-down results within the selected categories. As soon as I try and populate that text input it drops all the checkbox data (the categories are still checked). I am logging my parameters as the form is submitted and I get something like this:

{"keyword":"ER 8"}

The weird part is that if I type a keyword into the box, then start checking categories it picks up all the data and I get:

{"keyword":"ER 8","product_type":"Category","product_category":["Sub-Cat1","Sub-Cat2"]}

Anyone have any idea why the checkbox/radio fields are being dropped? Hopefully I have provided plenty of information, I'm happy to clarify anything I can.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After a bunch of trial and error, we figured out that the enter submit wasn't picking up the checkboxes for some reason. It was triggering the submit event and running the subsequent processes, just not feeding all the inputs to the function in the question. The solution was to intercept the enter key and then submit the form via jQuery .submit().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!