Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery create object from form fields

Tags:

How can I create a object with a form's fields and values?

like this one:

{   fields:    {       name: 'foo',       email: '[email protected]',       comment: 'wqeqwtwqtqwtqwet'          } } 

assuming the form looks like this:

<form>   <input type="text" name="name" value="foo" />   <input type="text" name="email" value="[email protected]" />   <textarea name="comment">wqeqwtwqtqwtqwet</textarea> </form> 

I need to know how can I do this for any form with a single function, not just a particular form.

like image 558
Alex Avatar asked Apr 09 '11 05:04

Alex


People also ask

How can we get form data as object?

get() The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.

How to create object in JQuery?

Another way to make objects in Javascript using JQuery , getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be: var box = {}; // my object var boxes = []; // my array $('div. test'). each(function (index, value) { color = $('p', this).

Which is the JQuery function that helps us to collect data from form fields and combine them to create a single JSON object?

The serializeArray() method creates an array of objects (name and value) by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself.

What will you use in JQuery if you wanted to convert the contents of a form elements into string for submission?

What will you use in jQuery if you wanted to convert the contents of a form element into string for submission? You can do this: var frm = $(document. myform); var data = JSON. stringify(frm.


1 Answers

You can do this:

var fields = {}; $("#theForm").find(":input").each(function() {     // The selector will match buttons; if you want to filter     // them out, check `this.tagName` and `this.type`; see     // below     fields[this.name] = $(this).val(); }); var obj = {fields: fields}; // You said you wanted an object with a `fields` property, so... 

Beware that forms can have fields with repeated names, and what you're trying to do doesn't support that. Also, the order of fields in HTML forms can be significant. (These are both reasons that serializeArray works the way it does.)

Note that normal HTML practice is to omit disabled fields. If you want to do that, check this.disabled before grabbing the value as well.


Note that the above (written two years ago) uses a jQuery pseudo-selector. I'm a bit surprised to find that I wrote that. As it says in the documentation for the :input pseudo-selector, using it means that jQuery can't hand off the selector to the browser's native querySelectorAll (which nearly all browsers now have).

Nowadays I'd probably write:

$("#theForm").find("input, textarea, select, button")... 

...if I wanted buttons, or if not then

$("#theForm").find("input, textarea, select")... 

...and then filter out input[type="button"] and input[type="submit"] inside the each. E.g. (no buttons at all):

$("#theForm").find("input, textarea, select").each(function() {     var inputType = this.tagName.toUpperCase() === "INPUT" && this.type.toUpperCase();     if (inputType !== "BUTTON" && inputType !== "SUBMIT") {         // ...include it, either it's an `input` with a different `type`         // or it's a `textarea` or a `select`...     } }); 
like image 132
T.J. Crowder Avatar answered Oct 02 '22 11:10

T.J. Crowder