Answer by khuong tran for How can I get form data with JavaScript/jQuery?
2024 solutionAlmost answer is correctly. But it take a lot of code, or it does not provide key/value object. Here is mineconst formData = new FormData(document.querySelector('form'));const data =...
View ArticleHow can I get form data with JavaScript/jQuery?
Is there a simple, one-line way to get the data of a form as it would be if it was to be submitted in the classic HTML-only way?For example:<form><input type="radio" name="foo" value="1"...
View ArticleAnswer by pixeline for How can I get form data with JavaScript/jQuery?
$("#form input, #form select, #form textarea").each(function() { data[theFieldName] = theFieldValue;});other than that, you might want to look at serialize();
View ArticleAnswer by chelmertz for How can I get form data with JavaScript/jQuery?
$('form').serialize() //this produces: "foo=1&bar=xxx&this=hi"demo
View ArticleAnswer by Boycott Russia for How can I get form data with JavaScript/jQuery?
Use $('form').serializeArray(), which returns an array:[ {"name":"foo","value":"1"}, {"name":"bar","value":"xxx"}, {"name":"this","value":"hi"}]Other option is $('form').serialize(), which returns a...
View ArticleAnswer by gamliela for How can I get form data with JavaScript/jQuery?
This will append all form fields to the JavaScript object "res":var res = {};$("#form input, #form select, #form textarea").each(function(i, obj) { res[obj.name] = $(obj).val();})
View ArticleAnswer by Nils for How can I get form data with JavaScript/jQuery?
use .serializeArray() to get the data in array format and then convert it into an object:function getFormObj(formId) { var formObj = {}; var inputs = $('#'+formId).serializeArray(); $.each(inputs,...
View ArticleAnswer by neuront for How can I get form data with JavaScript/jQuery?
Based on jQuery.serializeArray, returns key-value pairs.var data = $('#form').serializeArray().reduce(function(obj, item) { obj[item.name] = item.value; return obj;}, {});
View ArticleAnswer by Alexander for How can I get form data with JavaScript/jQuery?
You are all not fully correct. You cannot write:formObj[input.name] = input.value;Because this way if you have multiselect list - its values will be overwritten with the last one, since it's...
View ArticleAnswer by Clox for How can I get form data with JavaScript/jQuery?
Here's a really simple and short soluton that even doesn't require Jquery.var formElements=document.getElementById("myForm").elements; var postData={};for (var i=0; i<formElements.length; i++) if...
View ArticleAnswer by numediaweb for How can I get form data with JavaScript/jQuery?
You can also use the FormData Objects; The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. Its primarily intended for use in sending form data, but can be used...
View ArticleAnswer by mikemaccana for How can I get form data with JavaScript/jQuery?
Updated answer for 2014:HTML5 FormData does thisvar formData = new FormData(document.querySelector('form'))You can then post formData exactly as it is - it contains all names and values used in the form.
View ArticleAnswer by user1101791 for How can I get form data with JavaScript/jQuery?
This method should do it. It serializes the form data and then converts them to an object. Takes care of groups of checkboxes as well.function getFormObj(formId) { var formParams = {}; $('#'+ formId)...
View ArticleAnswer by Cezary Wojtkowski for How can I get form data with JavaScript/jQuery?
I wrote a library to solve this very problem: JSONForms. It takes a form, goes through each input and builds a JSON object you can easily read.Say you have the following form:<form...
View ArticleAnswer by RobKohr for How can I get form data with JavaScript/jQuery?
If you are using jQuery, here is a little function that will do what you are looking for.First, add an ID to your form (unless it is the only form on the page, then you can just use 'form' as the dom...
View ArticleAnswer by George John for How can I get form data with JavaScript/jQuery?
var formData = new FormData($('#form-id'));params = $('#form-id').serializeArray();$.each(params, function(i, val) { formData.append(val.name, val.value);});
View ArticleAnswer by Kyle Falconer for How can I get form data with JavaScript/jQuery?
Here is a working JavaScript only implementation which correctly handles checkboxes, radio buttons, and sliders (probably other input types as well, but I've only tested these).function...
View ArticleAnswer by Marcos Costa for How can I get form data with JavaScript/jQuery?
you can use this function for have an object or a JSON from form.for use it:var object = formService.getObjectFormFields("#idform"); function getObjectFormFields(formSelector) { ///...
View ArticleAnswer by István for How can I get form data with JavaScript/jQuery?
I wrote a function that takes care of multiple checkboxes and multiple selects. In those cases it returns an array.function getFormData(formId) { return $('#'+ formId).serializeArray().reduce(function...
View ArticleAnswer by 郭润民 for How can I get form data with JavaScript/jQuery?
function getFormData($form){ var unindexed_array = $form.serializeArray(); var indexed_array = {}; $.map(unindexed_array, function(n, i){ if(indexed_array[n['name']] == undefined){...
View Article