How to convert form data to JavaScript object with jQuery?

0 votes
How do I convert all elements of my form to a JavaScript object?

I'd like to have some way of automatically building a JavaScript object from my form, without having to loop over each element. I do not want a string, as returned by $('#formid').serialize();, nor do I want the map returned by $('#formid').serializeArray();
Apr 28 in JQuery by kartik
• 11,890 points
39 views

1 answer to this question.

0 votes

Hello @kartik,

You can use:

function form_to_json (selector) {
  var ary = $(selector).serializeArray();
  var obj = {};
  for (var a = 0; a < ary.length; a++) obj[ary[a].name] = ary[a].value;
  return obj;
}

Output:

{"myfield": "myfield value", "passwordfield": "mypasswordvalue"}

Thank You!!

answered Apr 28 by Niroj
• 23,950 points

Related Questions In JQuery

0 votes
1 answer

How to import jQuery UI using ES6/ES7 syntax?

Hii, Add a alias in webpack config: resolve: { ...READ MORE

answered Apr 28 in JQuery by Niroj
• 23,950 points
71 views
0 votes
1 answer

How to check if a jQuery plugin is loaded?

Hello @kartik, for the plugins that doesn't use ...READ MORE

answered Apr 28 in JQuery by Niroj
• 23,950 points
18 views
0 votes
1 answer

How to remove close button on the jQuery UI dialog?

Hii, I found this worked for me in the end ...READ MORE

answered May 12 in JQuery by Niroj
• 23,950 points
39 views
0 votes
1 answer

How to set cache false for getJSON in jQuery?

Hello @kartik, Your code just needs a trigger ...READ MORE

answered May 12 in JQuery by Niroj
• 23,950 points
35 views
0 votes
1 answer

What is Laravel framework? Why one should use Laravel?

Laravel is a PHP web-framework; it utilized ...READ MORE

answered Mar 17 in Laravel by Niroj
• 23,950 points
71 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 23,950 points
67 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17 in Laravel by Niroj
• 23,950 points
32 views
0 votes
1 answer

What are the important directories used in a common Laravel application

Hey @Kartik. Directories used in a common Laravel ...READ MORE

answered Mar 17 in Laravel by Niroj
• 23,950 points
34 views
0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello, Here is the syntax using jQuery $.get $.get(url, data, ...READ MORE

answered Apr 28 in JQuery by Niroj
• 23,950 points
36 views
0 votes
1 answer

How to move an item programmatically with jQuery sortable while still triggering events?

Hello, $("selector").trigger("sortupdate"); you will have to  pass as second argument  ...READ MORE

answered May 29 in JQuery by Niroj
• 23,950 points
30 views