{"id":676,"date":"2022-03-20T00:00:10","date_gmt":"2022-03-20T00:00:10","guid":{"rendered":"https:\/\/code-boxx.com\/?p=676"},"modified":"2023-06-11T05:46:45","modified_gmt":"2023-06-11T05:46:45","slug":"get-html-form-data-javascript","status":"publish","type":"post","link":"https:\/\/code-boxx.com\/get-html-form-data-javascript\/","title":{"rendered":"Get HTML Form Data In Javascript (Simple Examples)"},"content":{"rendered":"<p>Welcome to a quick tutorial on how to get the HTML form data in Javascript. So you have an HTML form but want to do the processing in Javascript instead?<\/p>\n<div class=\"sec-head answer\">\n<p>To get the HTML form data in Javascript:<\/p>\n<ol>\n<li>Create the HTML form as usual &#8211; Give the form an ID, and attach a name to the input fields.\n<ul>\n<li><code>&lt;form id=\"demo\"&gt;<\/code><\/li>\n<li><code>&lt;input name=\"name\"&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Create a form data object, and feed the HTML form in as a parameter.\n<ul>\n<li><code>var form = document.getElementById(\"demo\");<\/code><\/li>\n<li><code>var data = new FormData(form);<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Lastly, manually append more keys\/values if required &#8211; <code>data.append(\"KEY\", \"VALUE\");<\/code><\/li>\n<\/ol>\n<\/div>\n<p>That covers the quick basics, but read on for more examples!<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-101\"><\/div>\n<p>&nbsp;<\/p>\n<h2 class=\"in-head-d\">TLDR &#8211; QUICK SLIDES<\/h2>\n<p>[web_stories_embed url=&#8221;https:\/\/code-boxx.com\/web-stories\/how-to-get-html-form-data-in-javascript\/&#8221; title=&#8221;How To Get HTML Form Data In Javascript&#8221; poster=&#8221;https:\/\/code-boxx.com\/wp-content\/uploads\/2021\/11\/STORY-JS-20230518.webp&#8221; width=&#8221;360&#8243; height=&#8221;600&#8243; align=&#8221;center&#8221;]<\/p>\n<p style=\"text-align: center;\">Fullscreen Mode &#8211; <a href=\"https:\/\/code-boxx.com\/web-stories\/how-to-get-html-form-data-in-javascript\/\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2 class=\"in-head-d\">TABLE OF CONTENTS<\/h2>\n<div class=\"toc\">\n<div><a href=\"#sec-get\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-file-text.svg\" width=\"36\" height=\"36\" \/>JS Get Form Data<\/a><\/div>\n<div><a href=\"#sec-download\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-download.svg\" width=\"36\" height=\"36\" \/>Download &amp; Notes<\/a><\/div>\n<div><a href=\"#sec-extra\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-plus-square.svg\" width=\"36\" height=\"36\" \/>Extra Bits &amp; Links<\/a><\/div>\n<div><a href=\"#sec-end\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-thumbs-up.svg\" width=\"36\" height=\"36\" \/>The End<\/a><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-102\"><\/div>\n<p>&nbsp;<\/p>\n<div id=\"sec-get\" class=\"sec-head\" style=\"background: #e0f3ff;\">\n<h2 class=\"in-head-d\">JAVASCRIPT GET FORM DATA<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-file-text.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>All right, let us now get into the examples of how to get the form data in Javascript.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE 1) GET FORM DATA &amp; AJAX POST<\/span><\/h3>\n<div class=\"ref-head\">1-basic-post.html<\/div>\n<pre><code>&lt;!-- (A) HTML FORM --&gt;\r\n&lt;form id=\"demoA\" onsubmit=\"return doForm()\"&gt;\r\n  &lt;input type=\"text\" name=\"name\" required value=\"Jon Doe\"&gt;\r\n  &lt;input type=\"email\" name=\"email\" required value=\"jon@doe.com\"&gt;\r\n  &lt;input type=\"submit\" value=\"Go!\"&gt;\r\n&lt;\/form&gt;\r\n \r\n&lt;!-- (B) JS PROCESSING --&gt;\r\n&lt;script&gt;\r\nfunction doForm () {\r\n  \/\/ (B1) FORM DATA - AUTO GET ALL FIELDS FROM \"#DEMOA\"\r\n  var data = new FormData(document.getElementById(\"demoA\"));\r\n\r\n  \/\/ (B2) AJAX FETCH\r\n  fetch(\"SERVER-SCRIPT\", { method:\"post\", body:data })\r\n  .then(res =&gt; res.text())\r\n  .then(txt =&gt; console.log(txt))\r\n  .catch(err =&gt; console.error(err));\r\n \r\n  \/\/ (B3) PREVENT DEFAULT FORM SUBMIT\r\n  return false;\r\n}\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>This is the &#8220;complete example&#8221; of the above introduction snippet. All we need is to:<\/p>\n<ol style=\"list-style-type: upper-alpha;\">\n<li>Create the HTML <code>&lt;form&gt;<\/code> as usual.<\/li>\n<li>Create a <code>new FormData(HTML FORM)<\/code> object, and submit it to the server &#8220;as usual&#8221;.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-103\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE 2) GET FORM DATA &amp; URL PARAMS<\/span><\/h3>\n<div class=\"ref-head\">2-basic-params.html<\/div>\n<pre><code>&lt;!-- (A) HTML FORM --&gt;\r\n&lt;form id=\"demoB\" onsubmit=\"return doForm()\"&gt;\r\n  &lt;input type=\"text\" name=\"name\" required value=\"Joe Doe\"&gt;\r\n  &lt;input type=\"email\" name=\"email\" required value=\"joe@doe.com\"&gt;\r\n  &lt;input type=\"submit\" value=\"Go!\"&gt;\r\n&lt;\/form&gt;\r\n \r\n&lt;!-- (B) JS PROCESSING --&gt;\r\n&lt;script&gt;\r\nfunction doForm () {\r\n  \/\/ (B1) FORM DATA - AUTO GET ALL FIELDS FROM \"#DEMOB\"\r\n  var data = new FormData(document.getElementById(\"demoB\"));\r\n \r\n  \/\/ (B2) TO URL PARAMS\r\n  var params = new URLSearchParams(data).toString();\r\n \r\n  \/\/ (B3) AJAX FETCH\r\n  fetch(\"SERVER-SCRIPT?\" + params)\r\n  .then(res =&gt; res.text())\r\n  .then(txt =&gt; console.log(txt))\r\n  .catch(err =&gt; console.error(err));\r\n\r\n  \/\/ (B4) OR REDIRECT - MIGHT AS WELL JUST SUBMIT THE FORM?\r\n  \/\/ location.href = \"SERVER-SCRIPT?\" + params;\r\n \r\n  \/\/ (B5) PREVENT DEFAULT FORM SUBMIT\r\n  return false;\r\n}\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>This is an alternative for the folks who want to do <code>GET<\/code> instead of <code>POST<\/code> &#8211;<\/p>\n<ul>\n<li><strong>(A &amp; B1)<\/strong> The &#8220;usual HTML form&#8221; and create <code>var data = new FormData(HTML FORM)<\/code>.<\/li>\n<li><strong>(B2)<\/strong> Then, &#8220;convert&#8221; into <code>var params = new URLSearchParams(data)<\/code>.<\/li>\n<li><strong>(B3)<\/strong> Attach the parameters to the end of the URL &#8211; <code>\"http:\/\/site.com?\" + params.toString()<\/code>, and submit the form.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-106\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE 3) MORE FORM DATA<\/span><\/h3>\n<div class=\"ref-head\">3-append-more.html<\/div>\n<pre><code>&lt;!-- (A) HTML FORM --&gt;\r\n&lt;form id=\"demoC\" onsubmit=\"return doForm()\"&gt;\r\n  &lt;input type=\"text\" name=\"name\" required value=\"Joy Doe\"&gt;\r\n  &lt;input type=\"email\" name=\"email\" required value=\"joy@doe.com\"&gt;\r\n  &lt;div id=\"birthday\"&gt;12 Mar 2077&lt;\/div&gt;\r\n  &lt;input type=\"submit\" value=\"Go!\"&gt;\r\n&lt;\/form&gt;\r\n \r\n&lt;!-- (B) JS PROCESSING --&gt;\r\n&lt;script&gt;\r\nfunction doForm () {\r\n  \/\/ (B1) FORM DATA - AUTO GET ALL FIELDS FROM \"#DEMOC\"\r\n  var data = new FormData(document.getElementById(\"demoC\"));\r\n \r\n  \/\/ (B2) MANUALLY APPEND MORE\r\n  data.append(\"birthday\", document.getElementById(\"birthday\").innerText);\r\n  data.append(\"gender\", \"female\");\r\n  data.append(\"key\", \"value\");\r\n \r\n  \/\/ (B3) FORM DATA YOGA\r\n  data.delete(\"key\");\r\n  var name = data.get(\"name\"); \/\/ Joy Doe\r\n  var hasEmail = data.has(\"email\"); \/\/ true\r\n  for (let k of data.keys()) { console.log(k); }\r\n  for (let v of data.values()) { console.log(v); }\r\n  for (let [k, v] of data.entries()) { console.log(k, v); }\r\n \r\n  \/\/ (B4) PREVENT DEFAULT FORM SUBMIT\r\n  return false;\r\n}\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>One last example to address a few common concerns.<\/p>\n<ul>\n<li><strong>(A)<\/strong> Take note that the HTML form now has a &#8220;custom <code>&lt;div id=\"birthday\"&gt;<\/code> widget&#8221;.<\/li>\n<li><strong>(B1)<\/strong> Since the &#8220;custom widget&#8221; is not the usual <code>&lt;input&gt;<\/code>, <code>var data = new FormData(FORM)<\/code> will not automatically adapt data from it.<\/li>\n<li><strong>(B2)<\/strong> To &#8220;fix&#8221; that, we can use <code>data.append(\"KEY\", \"VALUE\")<\/code> to manually add more data entries.<\/li>\n<li><strong>(B3)<\/strong> Now, the <code>FormData<\/code> object is not some &#8220;black hole&#8221;. You put data in and have no idea what&#8217;s inside.\n<ul style=\"list-style-type: circle;\">\n<li><code>get(KEY)<\/code> Returns the value of the specified <code>KEY<\/code>.<\/li>\n<li><code>has(KEY)<\/code> Checks if the dataset contains <code>KEY<\/code>.<\/li>\n<li><code>key()<\/code> Return all the keys in an array.<\/li>\n<li><code>values()<\/code> Return all the values in an array.<\/li>\n<li><code>entries()<\/code> Returns all the keys and values in an array.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-107\"><\/div>\n<p>&nbsp;<\/p>\n<div id=\"sec-download\" class=\"sec-head\" style=\"background: #ffe8de;\">\n<h2 class=\"in-head-d\">DOWNLOAD &amp; NOTES<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-download.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>Here is the download link to the example code, so you don&#8217;t have to copy-paste everything.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">SORRY FOR THE ADS...<\/span><\/h3>\r\n<p>But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a \"paid scripts\" business, and I don't \"block people with Adblock\". Every little bit of support helps.<\/p>\r\n\r\n<p><a class=\"ast-button\" href=\"https:\/\/www.paypal.com\/paypalme\/wstoh\/5\" target=\"_blank\" rel=\"nofollow noopener\">Buy Me A Coffee<\/a> <a class=\"ast-button\" href=\"https:\/\/payhip.com\/codeboxx\" target=\"_blank\" rel=\"nofollow noopener\">Code Boxx eBooks<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE CODE DOWNLOAD<\/span><\/h3>\n<p><a href=\"https:\/\/gist.github.com\/code-boxx\/d788ea71ab12c51b777d66294e966af6\" target=\"_blank\" rel=\"noopener noreferrer\">Click here for the source code on GitHub gist<\/a>, just click on &#8220;download zip&#8221; or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"sec-extra\" class=\"sec-head\" style=\"background: #f8d5ff;\">\n<h2 class=\"in-head-d\">EXTRA BITS &amp; LINKS<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-plus-square.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>That&#8217;s all for the tutorial, and here is a small section on some extras and links that may be useful to you.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">LINKS &amp; REFERENCES<\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/getElementById\" target=\"_blank\" rel=\"noopener\">Get Element By Id<\/a> &#8211; MDN<\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/FormData\" target=\"_blank\" rel=\"noopener\">Form Data<\/a> &#8211; MDN<\/li>\n<li><a href=\"https:\/\/code-boxx.com\/submit-form-without-refreshing-page\/\" target=\"_blank\" rel=\"noopener\">Submit Form Without Reloading<\/a> &#8211; Code Boxx<\/li>\n<li><a href=\"https:\/\/code-boxx.com\/display-message-after-submitt-html-form\/\" target=\"_blank\" rel=\"noopener\">Display Message After Form Submit<\/a> &#8211; Code Boxx<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">INFOGRAPHIC CHEAT SHEET<\/span><\/h3>\n<figure style=\"width: 324px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2022\/03\/JS-form-data-20230305-R.webp\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2022\/03\/JS-form-data-20230305-sm-R.webp\" alt=\"\" width=\"324\" height=\"600\" \/><\/a><figcaption class=\"wp-caption-text\"><span style=\"font-size: 16px;\">Get HTML Form Data In Javascript (Click To Enlarge)<\/span><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<div id=\"sec-end\" class=\"sec-head\" style=\"background: #f1f1f1;\">\n<h2 class=\"in-head-d\">THE END<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-thumbs-up.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to a quick tutorial on how to get the HTML form data in Javascript. So you have an HTML form but want to do the processing in Javascript instead? To get the HTML form data in Javascript: Create the HTML form as usual &#8211; Give the form an ID, and attach a name to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20777,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18,15],"tags":[],"class_list":["post-676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html-css","category-js"],"_links":{"self":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/676"}],"collection":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":0,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media\/20777"}],"wp:attachment":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}