{"id":3174,"date":"2011-05-12T14:43:43","date_gmt":"2011-05-12T18:43:43","guid":{"rendered":"http:\/\/www.useragentman.com\/blog\/?p=3174"},"modified":"2012-05-20T08:45:41","modified_gmt":"2012-05-20T12:45:41","slug":"fixing-oninput-in-ie9-using-html5widgets","status":"publish","type":"post","link":"http:\/\/www.useragentman.com\/blog\/2011\/05\/12\/fixing-oninput-in-ie9-using-html5widgets\/","title":{"rendered":"Fixing <code>oninput<\/code> in IE Using html5Widgets"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2011\/05\/oninput.png\" alt=\"\" title=\"oninput\" width=\"250\" height=\"140\" class=\"alignleft size-full wp-image-3197\" \/> In my <a href=\"\/blog\/2011\/05\/10\/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways\/#comment-12167\">previous blog post<\/a>, I wished that the <code>onforminput<\/code> event was not deprecated in the HTML5 specification.  I have used this attribute in the past to show values in range elements and show calculations of values inside a form, and thought it was perfect for those who know little JavaScript to implement features like this very easily. <a href=\"http:\/\/www.boogdesign.com\/b2evo\/\">Rob Crowther<\/a> made <a href=\"\/blog\/2011\/05\/10\/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways\/#comment-12167\">pointed out<\/a> that the <code>oninput<\/code> event, which has a similar function as <code>onforminput<\/code> the main argument behind dropping <code>onforminput<\/code> event.  Makes sense, and I was happy &mdash; it works in all modern web browsers, including IE.  But as you would guess, there has to be at least one problem with a cool bit of new web technology when it comes to cross-browser implementation.<\/p>\n<h2>What are the issues?<\/h2>\n<p>IE has three issues related to <code>oninput<\/code>:<\/p>\n<ol>\n<li><code>oninput<\/code> is not supported at all in IE8 and lower.<\/li>\n<li>IE9 does not allow referring to form fields directly in the <code>oninput<\/code> expression.  For example, this code works in all browsers except IE9:<br \/>\n<blockquote class=\"code\">\n<pre>\r\n&lt;form name=\"f1\" <span class=\"hilite\">oninput=\"output.value = parseFloat(darkness.value);\"<\/span>&gt;\r\n    Darkness Level:\r\n    &lt;input type=\"range\" name=\"darkness\" 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>This is not so bad, since one could refactor the event code like this:<\/p>\n<blockquote class=\"code\">\n<pre>\r\n&lt;form name=\"f1\" \r\n  <span class=\"hilite\">oninput=\"document.getElementById('output').innerHTML = parseFloat(darkness.value);\"<\/span>&gt;\r\n    Darkness Level:\r\n    &lt;input type=\"range\" name=\"darkness\" value=\"0\" min=\"0\" max=\"255\"&gt;\r\n    &lt;output id=\"output\"&gt;   \r\n&lt;\/form&gt;\r\n<\/pre>\n<\/blockquote>\n<\/li>\n<li>If you use the backspace key while entering in a value, IE9 doesn&#8217;t fire the <code>oninput<\/code> event.  This, in my opinion, is really bad, because it does affect how the form behaves.  For example, take the following form in IE9:<br \/>\n<form oninput=\"document.getElementById('tax').innerHTML = (parseFloat(amount.value) * parseFloat(price.value) * 0.15).toFixed(2); document.getElementById('total').innerHTML = (parseFloat(amount.value) * parseFloat(price.value) * 1.15).toFixed(2)\">\n<table class=\"formTable\">\n<tbody>\n<tr>\n<th>\nAmount:\n<\/th>\n<td>\n<input name=\"amount\" value=\"1\" min=\"1\" max=\"100\" type=\"number\">\n<\/td>\n<\/tr>\n<tr>\n<th>\nItem:\n<\/th>\n<td>\n<p><input name=\"price\" value=\"299.99\" min=\"1\" step=\"0.01\" type=\"number\">\n<\/td>\n<\/tr>\n<tr>\n<th>Tax:<\/th>\n<td><output id=\"tax\">45.00<\/output><\/td>\n<\/tr>\n<tr>\n<th>Total:<\/th>\n<td><output id=\"total\">344.99<\/output>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/form>\n<p>As the user types in values for the amount and price into the form, the <code>oninput<\/code> event calculates the tax and total.  <strong>However, if the user is entering a value into, say, the price and make a mistake and press the backspace, IE9 will not update the tax and total.<\/strong>  Your application will give erroneous information, the user will be surprised at the total being larger than he expected, says &#8220;screw this&#8221; and leaves the application.  Your company loses the big sale, your boss fires you, and <strong>next thing you know you are on a street, or worse, coding <a href=\"http:\/\/drdobbs.com\/architecture-and-design\/210602491\">COBOL<\/a> on a 30 year old IBM PC running MS-DOS 1.0 in some fishing tackle store.<\/strong>  Not pretty.<\/li>\n<\/ol>\n<h2>Oh The Humanity! How Can I Prevent Such a Fate?<\/h2>\n<p>Since I hate COBOL, I decided to update my html5Widgets library to:<\/p>\n<ol>\n<li>Add support for <code>oninput<\/code> for browsers that don&#8217;t support it (e.g. IE7 and 8)<\/li>\n<li>Force IE9 to fire a form&#8217;s <code>oninput<\/code> when the backspace and delete keys are pressed inside any of the <code>input<\/code> nodes.<\/li>\n<li>Force IE9 to fire a form&#8217;s <code>oninput<\/code> when the <code>cut<\/code> event is fired on any of the <code>input<\/code> nodes.<\/li>\n<\/ol>\n<p>Using the link below, you can see an example similar to the one above using html5Widgets to fix these issues.<\/p>\n<p><a class=\"exampleLink\" href=\"\/examples\/html5Widgets\/tests\/html5Widgets\/outputWithOninput.html\">Let&#8217;s see html5Widgets give IE some <code>oninput<\/code> goodness<\/a><\/p>\n<p>In order to do this, I used ideas from <a href=\"http:\/\/blog.danielfriesen.name\">Daniel Friesen&#8217;s<\/a> blog post, <em><a href=\"http:\/\/blog.danielfriesen.name\/2010\/02\/16\/html5-browser-maze-oninput-support\/\">A HTML5 Browser Maze, Oninput Support<\/a><\/em> (thanks to <a href=\"www.paulirish.com\">Paul Irish<\/a> for pointing me into that direction).  The result works rather well.  Note that I did not fix the IE9 &#8220;cannot use <code>.value<\/code>, must use <code>.innerHTML<\/code>&#8221; bug described earlier. It&#8217;s a little bit more verbose, but for now it&#8217;s what a developer needs to do for true cross-browser <code>oninput<\/code> support. I have also kept <code>onforminput<\/code> support in the code, just in case anyone has used it in the past (e.g. me).  To ensure the best experience, I wouldn&#8217;t use both events on the same page.  <\/p>\n<p>Note also I did not add support for <code>oninput<\/code> in any element other than <code>form<\/code>.  I may do this after further research in the way it supported across browsers (for example, <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=195696\">Firefox has an issue with <code>oninput<\/code> being used on <code>textarea<\/code> elements<\/a>).<\/p>\n<h2>Download<\/h2>\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>oninput<\/code> support, from github.<\/a><\/p>\n<h2>Further Reading<\/h2>\n<ul>\n<li><a href=\"http:\/\/blog.danielfriesen.name\">Daniel Friesen&#8217;s<\/a> blog post, <em><a href=\"http:\/\/blog.danielfriesen.name\/2010\/02\/16\/html5-browser-maze-oninput-support\/\">A HTML5 Browser Maze, Oninput Support<\/a><\/em>.<\/li>\n<li><a href=\"http:\/\/help.dottoro.com\/ljhxklln.php\">oninput event | input event<\/a> from the <a href=\"http:\/\/help.dottoro.com\/\">Dottoro Web Reference<\/a><\/li>\n<li><a href=\"http:\/\/mathiasbynens.be\/notes\/oninput\">Using the oninput event with onkeydown as its fallback<\/a> by the ever talented <a href=\"http:\/\/mathiasbynens.be\/\">Mathias Bynens<\/a>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2011\/05\/oninput.png\" alt=\"\" title=\"oninput\" width=\"250\" height=\"140\" class=\"alignleft size-full wp-image-3197\" \/> Even though <code>onforminput<\/code> is deprecated, <code>oninput<\/code> will allow developers to do dynamic calculations on forms with little JavaScript knowledge.  Unfortunately, it doesn&#8217;t work in IE8 and lower, and there are some small bugs in IE9&#8217;s implementation when trying to delete characters inside a form field. So I implemented a fix for these issues with an update to my cross-browser HTML5 Forms polyfill, html5Widgets.<\/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,7,119,1],"tags":[113,103,117,112,111,110,114,116,115],"class_list":["post-3174","post","type-post","status-publish","format-standard","hentry","category-events-html5","category-events","category-forms","category-html","category-html5","category-javascript","category-polyfills","category-uncategorized","tag-html5widgets","tag-ie9","tag-internet-explorer-9","tag-javascript-events","tag-onforminput","tag-oninput","tag-oninput-backspace","tag-oninput-cut","tag-oninput-delete"],"_links":{"self":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3174","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=3174"}],"version-history":[{"count":57,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3174\/revisions"}],"predecessor-version":[{"id":3225,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/3174\/revisions\/3225"}],"wp:attachment":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/media?parent=3174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/categories?post=3174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/tags?post=3174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}