{"id":647,"date":"2014-08-17T19:39:06","date_gmt":"2014-08-17T19:39:06","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=647"},"modified":"2015-08-17T10:58:28","modified_gmt":"2015-08-17T10:58:28","slug":"serialize-form-not-working-in-jquery","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/serialize-form-not-working-in-jquery\/","title":{"rendered":"Serialize form not working in jQuery"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[647]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>So, <a href=\"http:\/\/stackoverflow.com\/questions\/6580450\/serialize-form-not-working-in-jquery\">my question<\/a>\u00a0wa<span style=\"color: #000000;\">s if, for example, we have this HTML form code:<\/span><\/p>\n<pre class=\"lang:default decode:true\">&lt;form action=\"#\" id=\"gamesForm\"&gt;\r\n    &lt;p&gt;                                                        \r\n        &lt;input id=\"gName\" type=\"text\" class=\"medium\" \/&gt;\r\n        &lt;span class=\"notification information\"&gt;Game name&lt;\/span&gt;\r\n    &lt;\/p&gt;\r\n\r\n    &lt;p&gt;                            \r\n        &lt;span class=\"notification information\"&gt;Enabled:&lt;\/span&gt;\r\n        &lt;input id=\"gEnabled\" type=\"checkbox\" \/&gt;              \r\n    &lt;\/p&gt;\r\n\r\n\r\n    &lt;br \/&gt;\r\n    &lt;!--Additional data for extra type--&gt;\r\n    &lt;div id=\"extraAdditionalData\" class=\"hidden\"&gt;                            \r\n        &lt;p&gt;\r\n            &lt;input id=\"rRacers\" type=\"text\" class=\"medium\" \/&gt;\r\n            &lt;span class=\"notification information\"&gt;Racers&lt;\/span&gt;\r\n        &lt;\/p&gt;\r\n\r\n        &lt;p&gt;\r\n            &lt;input id=\"rVideoSet\" type=\"text\" class=\"medium\" \/&gt;\r\n            &lt;span class=\"notification information\"&gt;Video set&lt;\/span&gt;\r\n        &lt;\/p&gt;                                                         \r\n     &lt;\/div&gt;                \r\n&lt;\/form&gt;\r\n\r\n&lt;a href=\"#\" id=\"saveConfiguration\" class=\"graybuttonBig\"&gt;Save everything!&lt;\/a&gt;<\/pre>\n<p>And this js code:<\/p>\n<pre class=\"lang:default decode:true \">$(document).ready(function(){\r\n    $(\"#saveConfiguration\").click(function(){\r\n        alert( $(\"form\").serialize() );   \r\n    });\r\n});<\/pre>\n<p>How come that all I get in the alert is an empty string?<\/p>\n<p>The answer, by user Felix Kling, was:<\/p>\n<blockquote><p>You have to give your form elements\u00a0<code>name<\/code>s!<\/p>\n<p>This is independent of jQuery. Every form element must have a\u00a0<code>name<\/code>\u00a0to be considered for form submission as\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#successful-controls\"><em>successful control<\/em><\/a>:<\/p>\n<blockquote style=\"font-style: normal;\"><p>A successful control is &#8220;valid&#8221; for submission. Every successful control has its\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#control-name\">control name<\/a>\u00a0paired with its\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#current-value\">current value<\/a>\u00a0as part of the submitted\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#form-data-set\">form data set<\/a>. A successful control must be defined within a\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#edef-FORM\"><code>FORM<\/code><\/a>\u00a0element and must have a\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#control-name\">control name<\/a>.<\/p><\/blockquote>\n<p>jQuery just ignores those elements that don&#8217;t have a name (or, depending on how it gets the elements, it might not even see them as the form itself has no reference to them).<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=647"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/647\/revisions"}],"predecessor-version":[{"id":1717,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/647\/revisions\/1717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}