{"id":3065,"date":"2011-05-10T15:16:01","date_gmt":"2011-05-10T19:16:01","guid":{"rendered":"http:\/\/www.useragentman.com\/blog\/?p=3065"},"modified":"2012-05-20T08:48:05","modified_gmt":"2012-05-20T12:48:05","slug":"is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways","status":"publish","type":"post","link":"http:\/\/www.useragentman.com\/blog\/2011\/05\/10\/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways\/","title":{"rendered":"Is <code>onforminput<\/code> Deprecated in HTML5 Forms? (And Why Should I Care Anyways?)"},"content":{"rendered":"<div class=\"importantNotes\">\n<h3>Update:<\/h3>\n<p><strong>Newer versions of all browsers support the <code>form<\/code> element&#8217;s <code>oninput<\/code> attribute, which replaces the functionality of <code>onforminput<\/code>, and as a result I wrote a related article <a href=\"https:\/\/www.useragentman.com\/blog\/2011\/05\/12\/fixing-oninput-in-ie9-using-html5widgets\/\">Fixing <code>oninput<\/code> in IE Using html5Widgets<\/a>.<\/strong>  Thanks to <a href=\"http:\/\/www.boogdesign.com\/b2evo\/\">Rob Crowther<\/a> for setting me straight and providing <a href=\"http:\/\/jsfiddle.net\/robertc\/YCEKQ\/\">a great example<\/a>, which <a href=\"http:\/\/jsfiddle.net\/dYSS6\/\">I have forked<\/a> to work for IE9.  I will refactor html5Widgets to support <code>oninput<\/code> in IE very soon.  I will leave this article as is for the benefit of anyone who is Googling for <code>onforminput<\/code> and why it was deprecated.\n<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2011\/05\/deprecated.png\" alt=\"\" title=\"deprecated\" width=\"250\" height=\"140\" class=\"alignleft size-full wp-image-3112\" \/><\/p>\n<p>I was just doing some regression testing on <a href=\"https:\/\/www.useragentman.com\/blog\/2010\/07\/27\/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets\/\">html5Widgets<\/a>, and noticed that the <code>onforminput<\/code> event wasn&#8217;t working in Firefox 4.  This didn&#8217;t make sense to me: I designed html5Widgets to detect if the <code>output<\/code> tag is supported, and if not, add support for it.  I believed that if a browser supported the <code>output<\/code> tag, it also had to support the <code>onforminput<\/code> event, since I assumed that this was one of the main reasons of having the <code>output<\/code> tag in the first place.  However, <strong>Firefox 4 supports the <code>output<\/code> tag, but doesn&#8217;t support <code>onforminput<\/code>.<\/strong>  After doing a bit of light research, I came across this <a href=\"http:\/\/www.w3.org\/Bugs\/Public\/show_bug.cgi?id=11129\">this bug report<\/a> that seems to indicate that <code>onforminput<\/code> is deprecated.  This article will discuss<strong> what <code>onforminput<\/code> does<\/strong>, why it is very <strong>useful to designers are not so strong in JavaScript<\/strong>, how one can <strong>add support for <code>onforminput<\/code> to browsers that don&#8217;t<\/strong>, and why I really <strong>hope someone in power adds it back to the HTML5 Forms specification,<\/strong> or at least something very similar.<\/p>\n<h2>Why Do I Care<\/h2>\n<p>For those that don&#8217;t know what this is, <strong><code>onforminput<\/code> is an attribute<\/strong> that a web author can use on the <code>output<\/code> tag <strong>to do some calculations with the values in a form&#8217;s input fields<\/strong> and show that calculation to a user.  Right now, Opera is the only browser that natively supports it, but <a href=\"https:\/\/www.useragentman.com\/blog\/2010\/07\/27\/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets\/\">in an earlier post<\/a> I showed how one could use it with my html5Widgets polyfill library in all the other browsers.  How does it work? To illustrate a very simple use case, let&#8217;s say I have a <code>range<\/code> input field:<\/p>\n\r\n<table class=\"dataTable\">\r\n<thead>\r\n<tr>\r\n<th>HTML<\/th>\r\n<th>Output<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr>\r\n<td class=\"withCode\">\r\n<blockquote class=\"code\">\r\n<pre>\r\n&lt;form&gt;\r\n  Darkness Level:\r\n  &lt;input type=\"range\" name=\"darkness\"\r\n         value=\"0\" min=\"0\" max=\"255\" \/&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\r\n<\/blockquote>\r\n<\/td>\r\n<td>\r\n<form>\r\nDarkness Level: \r\n<input type=\"range\" \r\n       name=\"darkness\"\r\n       value=\"0\"\r\n       min=\"0\"\r\n       max=\"255\" \/>\r\n<\/form>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p>(Note the <code>range<\/code> element is <a href=\"http:\/\/remysharp.com\/2010\/10\/08\/what-is-a-polyfill\/\">polyfilled<\/a> with html5Widgets if your browser doesn&#8217;t support it).<\/p>\n<p>The <code>range<\/code> input type is a slider widget, and we use it here so the user can choose a number between 0 and 255.  The slider looks cool, but from a usability standpoint, there is no feedback of what value the user is sliding to.  It would make more sense to show a number next to the slider. which gives feedback to the user as to what the slider&#8217;s value is.  This is where <code>onforminput<\/code> comes in:<\/p>\n\r\n<table class=\"dataTable\">\r\n<thead>\r\n<tr>\r\n<th>HTML<\/th>\r\n<th>Output<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr>\r\n<td class=\"withCode\">\r\n<blockquote class=\"code\">\r\n<pre>\r\n&lt;form&gt;\r\n  Darkness Level:\r\n  &lt;input type=\"range\"\r\n     name=\"darkness\"\r\n     value=\"0\" min=\"0\" max=\"255\" \/&gt;\r\n  <span class=\"hilite\">&lt;output \r\n     onforminput=\"this.value = darkness.value\"\r\n  &gt;0&lt;\/output&gt; \/ 255<\/span>\r\n&lt;\/form&gt;\r\n\r\n<\/pre>\r\n<\/blockquote>\r\n<\/td>\r\n<td>\r\n<form>\r\n  Darkness Level:\r\n  <input type=\"range\" name=\"darkness\"\r\n         value=\"0\" min=\"0\" max=\"255\" \/>\r\n  <output \r\n         onforminput=\"this.value = darkness.value\"\r\n  >0<\/output> \/ 255\r\n<\/form>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p><strong>Immediate user feedback!  Great user experience! <em>And simple syntax!<\/em><\/strong>  Even a person who is not familiar with JavaScript can figure it out.  But it&#8217;s deprecated!  So, even though the <code>output<\/code> tag is supported by Firefox 4, <code>onforminput<\/code> is not.  Oh-noes!!!!<\/p>\n<p>Luckily, my html5Widgets library adds support for it in browsers that don&#8217;t, including Firefox 4.  I just made a small change that worked around an issue preventing Firefox from supporting it, and BAM!  Instant support.<\/p>\n<p>Another good (and common) example for using <code>output<\/code> would be a form for an online tool that calculates a total automatically.  Enter in values into the following form and see the calculations magically appear:<\/p>\n\r\n<form>\r\n<table class=\"formTable\">\r\n<tbody>\r\n<tr>\r\n<th>\r\nAmount:\r\n<\/th>\r\n<td>\r\n<input type=\"number\" name=\"amount\" value=\"1\" min=\"1\" max=\"100\" \/>\r\n<\/td>\r\n<\/tr>\r\n<tr>\r\n<th>\r\nItem:\r\n<\/th>\r\n<td>\r\n<input type=\"number\" name=\"price\" value=\"299.99\" min=\"1\" step=\"0.01\" \/>\r\n<\/td>\r\n<\/tr>\r\n<tr>\r\n<th>Tax:<\/th>\r\n<td><output onforminput=\"this.value = (parseFloat(amount.value) * parseFloat(price.value) * 0.15).toFixed(2)\"\r\n>0<\/output><\/td>\r\n<\/tr>\r\n<tr>\r\n<th>Total:<\/th>\r\n<td><output onforminput=\"this.value = (parseFloat(amount.value) * parseFloat(price.value) * 1.15).toFixed(2)\"\r\n>0<\/output>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/form>\r\n<h2>But, why is it depreciated in the first place?<\/h2>\n<p>It seems the decision has been made to remove <code>onforminput<\/code> from the HTML5 specification since it&#8217;s purpose can be easily done with capturing events (at least, that&#8217;s what I understood from <a href=\"http:\/\/lists.w3.org\/Archives\/Public\/public-webapps\/2010OctDec\/0177.html\">this thread about <code>onforminput<\/code> and <code>onformchange<\/code> on the public-webapps mailing list<\/a>).  <\/p>\n<p>While this is true, I think keeping it in would be a huge help to designers and developers.  I even think that having it declared inline like I do in the code examples above is useful.  Even though it breaks the separation of document structure and behavior, it allows a developer with practically no JavaScript knowledge to implement this simple feature in a form.  According to the <a href=\"http:\/\/www.w3.org\/Bugs\/Public\/show_bug.cgi?id=11129\">Bug Report<\/a>, designers shouldn&#8217;t have to use <code>onforminput<\/code> since they can do this instead: <\/p>\n<blockquote class=\"code\">\n<pre>\r\n&lt;form name=\"f1\"&gt;\r\nDarkness Level:\r\n<div class=\"hilite\">    &lt;script type=\"text\/javascript\"&gt;\r\n      document.addEventListener(\"input\",\r\n        function(e) {\r\n          if (document.forms.f1 == e.target.form)\r\n            e.target.form.output.value =\r\n              parseFloat(e.target.form.darkness.value);\r\n        }, true);\r\n    &lt;\/script&gt;<\/div>\r\n    &lt;input type=\"range\" name=\"darkness\"\r\n         value=\"0\" min=\"0\" max=\"255\" \/&gt;\r\n    &lt;output name=\"output\"&gt;\r\n  &lt;\/form&gt;\r\n<\/pre>\n<\/blockquote>\n<p><strong>This is uglier than your Mom&#8217;s mood when you don&#8217;t call her on Mother&#8217;s Day<\/strong> (not that I&#8217;m speaking from any recent experience). Even though it is dead easy for a JavaScript programmer to implement this without <code>onforminput<\/code>, some web designers wouldn&#8217;t have the knowledge or the patience to do so.  This is not a slight to those designers &mdash; after all, they can probably make better looking web pages than the average programmer. So I am keeping <code>onforminput<\/code> in html5Widgets.  It is really convenient and easy to understand.<\/p>\n<h2>A Note To Those In Power<\/h2>\n<p><strike>To anyone at the W3C who has the power to do so, I am politely asking to reconsider and <strong>please put <code>onforminput<\/code> back into the specification<\/strong> or <strong>at least replace it with something just as simple<\/strong> (the latter being attractive to those who want to continue separating the structure of the document (i.e. HTML) from the behavior (i.e. JavaScript)).<\/strike> As mention in the update on the top of the page, <code>oninput<\/code> is a great replacement for <code>onforminput<\/code> and it works in all modern browsers.  <\/p>\n<p>In my humble opinion, creating calculations the old-fashioned way with events and lines and lines of JavaScript is painful for designers who aren&#8217;t scripters.  After all, isn&#8217;t this the reason why <code>required<\/code> and <code>pattern<\/code> were created?<\/p>\n<p><a class=\"exampleLink\" href=\"https:\/\/github.com\/zoltan-dulac\/html5Forms.js\">Download the latest version of html5Forms.js, which includes HTML5Widgets with <code>onforminput<\/code> support, from github<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2011\/05\/deprecated.png\" alt=\"\" title=\"deprecated\" width=\"250\" height=\"140\" class=\"alignleft size-full wp-image-3112\" \/> The <code>onforminput<\/code> event has been stripped out of HTML5, and it makes me sad.  This article will show examples of how it works, how you can use it today in browsers that don&#8217;t support it, and hopefully will also present a good case as to why it should be put back into the HTML5 specification.  Should something this useful and easy-to-use be abandoned?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118,109,50,8,35,119],"tags":[],"class_list":["post-3065","post","type-post","status-publish","format-standard","hentry","category-events-html5","category-events","category-forms","category-html","category-html5","category-polyfills"],"_links":{"self":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/comments?post=3065"}],"version-history":[{"count":110,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3065\/revisions"}],"predecessor-version":[{"id":4888,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3065\/revisions\/4888"}],"wp:attachment":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/media?parent=3065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/categories?post=3065"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/tags?post=3065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}