Skip to content
Fix Code Error

How can I get form data with JavaScript/jQuery?

March 13, 2021 by Code Error
Posted By: Anonymous

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" checked="checked" />
    <input type="radio" name="foo" value="0" />
    <input name="bar" value="✘✘✘" />
    <select name="this">
        <option value="hi" selected="selected">Hi</option>
        <option value="ho">Ho</option>
</form>

Output:

{
    "foo": "1",
    "bar": "✘✘✘",
    "this": "hi"
}

Something like this is too simple, since it does not (correctly) include textareas, selects, radio buttons and checkboxes:

$("#form input").each(function () {
    data[theFieldName] = theFieldValue;
});

Solution

$('form').serialize() //this produces: "foo=1&bar=✘✘✘&this=hi"

demo

Answered By: Anonymous

Related Articles

  • Form field border-radius is not working only on the…
  • Active tab issue on page load HTML
  • Customize Bootstrap checkboxes
  • How To Check A Radio Button in Vue.JS 2 WITHOUT…
  • Jquery If radio button is checked
  • Is it possible to apply CSS to half of a character?
  • How do I limit the number of digits from 6 to 4 in…
  • How to call an Ember data function directly?
  • How to enforce required paper-radio-group in Polymer?
  • Is CSS Turing complete?
  • Filter array of objects through checkboxes
  • How does one validate paper-radio-group element with…
  • Trouble with boxes appearing/hiding based on selection
  • Vuetify/Vuejs radio group and checkbox group set to…
  • Vue v-model not reactive with BS4 radio button group
  • Polymer 1.x: Observers
  • How to make a radio button look like a toggle button
  • Polymer Paper-Radio-Group - how to have multiple…
  • Checkbox Check Event Listener
  • Polymer 1.0 Dom-repeat with paper-checkbox: uncheck…
  • backbone.js event binding is breaking radio buttons…
  • What does this symbol mean in JavaScript?
  • How to dynamically add and remove views with Ember.js
  • Polymer Paper radio group post value
  • Polymer 3: How to implement two way data binding for…
  • Radio Buttons ng-checked with ng-model
  • Check/Uncheck all the checkboxes in a table
  • Vue - Multiple radio inputs, checked value?
  • The radio button is not checked (turning blue)
  • Vue.js v-model with dynamic list of radio buttons
  • Js Calculation based on radio button, and some problems
  • Unable to import/export vuejs components
  • Get selected button's value from Polymer radio group…
  • paper-radio-group-changed not firing when a radio…
  • Vuetify radio button not appearing as checked
  • Use images instead of radio buttons
  • Vue-strap Radio Buttons not working with vue.js
  • How to use Servlets and Ajax?
  • Vuex dynamic checkboxes binding
  • CSS - How to Style a Selected Radio Buttons Label?
  • jQuery Mobile: document ready vs. page events
  • How to show multiple select options when user select…
  • Polymer 2.x paper-radio-group selectedValues not working
  • Sort table rows In Bootstrap
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How do I style (css) radio buttons and labels?
  • I'm trying to implement VeeValidate to check if at…
  • changing the style of radio button in vue js
  • paper-radio button does not update properly
  • What are the undocumented features and limitations…
  • Using :focus to style outer div?
  • Hidden property of a button in HTML
  • VueJS Master Checkbox Toggle with array values
  • paper-radio-button ripple persist after selection change
  • I'm working on pagination for my project. How do I…
  • How to use html template with vue.js
  • Set keyboard caret position in html textbox
  • Polymer 1.x: Databinding Arrays
  • Vue js bootstrap add animation while collapse
  • Show red border for all invalid fields after…
  • Latest jQuery version on Google's CDN
  • Polymer 2.0 nested iron-pages
  • Getting the value of a paper-radio-button inside a template
  • How do I use checkboxes in an IF-THEN statement in…
  • How to select radio button by clicking its div?
  • Not submit any value if checkbox is empty
  • How to make the checkbox unchecked by default always
  • OnChange event handler for radio button (INPUT…
  • change img src based on conditional statement vuejs
  • Bootstrap radio button "checked" flag
  • jQuery Array of all selected checkboxes (by class)
  • Input radio options to be displayed on click
  • laravel vuejs/axios put request Formdata is empty
  • How to respond to clicks on a checkbox in an…
  • How to reset after dynamically loading data using in…
  • How to create radio buttons and checkbox in swift (iOS)?
  • In jQuery, how do I get the value of a radio button…
  • What is your most productive shortcut with Vim?
  • Downloading jQuery UI CSS from Google's CDN
  • detect dynamically checkboxes/polymer elements…
  • *SOLVED* Component with multiple checkboxes and ONE v-model
  • How to select all checkboxes with jQuery?
  • How to validate checkbox group with Vuetify
  • problem with client server unix domain stream…
  • Generating a drop down list of timezones with PHP
  • Uncheck radio button
  • Git merge with force overwrite
  • default value selection in bootstrap multiselect…
  • "Thinking in AngularJS" if I have a jQuery background?
  • Keep button focused when using mulitple button groups
  • What's the proper value for a checked attribute of…
  • Changing Icon Color behind ListTile in an…
  • How to serialise only name of variant and ignore…
  • commandButton/commandLink/ajax action/listener…
  • How can I manually compile a svelte component down…
  • Select Tag Helper in ASP.NET Core MVC
  • Elegant ways to support equivalence ("equality") in…
  • gcc/g++: "No such file or directory"
  • Use CKeditor instance in Vue.js
  • What is causing this broken animation/transition in…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

startsWith() and endsWith() functions in PHP

Next Post:

How to reformat JSON in Notepad++?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error