Free Saeed Malekpour, Website Developer

Contact Me

@zoltandulac

Most Popular Posts

  • Cross Browser CSS Transforms – even in IE
  • How to Detect Font-Smoothing Using JavaScript
  • Cross Browser HTML5 Drag and Drop
  • @font-face in Depth
  • Installing Cygwin and FontForge for Windows
  • Cross Browser HTML5 Progress Bars In Depth
  • Creating Cross Browser HTML5 Forms Now, Using modernizr, webforms2 and html5Forms
  • Categories

    Fixing oninput in IE Using html5Widgets

    May 12th, 2011 by zoltan · 2 Comments

    In my previous blog post, I wished that the onforminput 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. Rob Crowther made pointed out that the oninput event, which has a similar function as onforminput the main argument behind dropping onforminput event. Makes sense, and I was happy — 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.

    What are the issues?

    IE has three issues related to oninput:

    1. oninput is not supported at all in IE8 and lower.
    2. IE9 does not allow referring to form fields directly in the oninput expression. For example, this code works in all browsers except IE9:
      <form name="f1" oninput="output.value = parseFloat(darkness.value);">
          Darkness Level:
          <input type="range" name="darkness" value="0" min="0" max="255">
          <output name="output">   
      </form>
      

      This is not so bad, since one could refactor the event code like this:

      <form name="f1" 
        oninput="document.getElementById('output').innerHTML = parseFloat(darkness.value);">
          Darkness Level:
          <input type="range" name="darkness" value="0" min="0" max="255">
          <output id="output">   
      </form>
      
    3. If you use the backspace key while entering in a value, IE9 doesn’t fire the oninput event. This, in my opinion, is really bad, because it does affect how the form behaves. For example, take the following form in IE9:
      Amount:
      Item:

      Tax: 45.00
      Total: 344.99

      As the user types in values for the amount and price into the form, the oninput event calculates the tax and total. 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. Your application will give erroneous information, the user will be surprised at the total being larger than he expected, says “screw this” and leaves the application. Your company loses the big sale, your boss fires you, and next thing you know you are on a street, or worse, coding COBOL on a 30 year old IBM PC running MS-DOS 1.0 in some fishing tackle store. Not pretty.

    Oh The Humanity! How Can I Prevent Such a Fate?

    Since I hate COBOL, I decided to update my html5Widgets library to:

    1. Add support for oninput for browsers that don’t support it (e.g. IE7 and 8)
    2. Force IE9 to fire a form’s oninput when the backspace and delete keys are pressed inside any of the input nodes.
    3. Force IE9 to fire a form’s oninput when the cut event is fired on any of the input nodes.

    Using the link below, you can see an example similar to the one above using html5Widgets to fix these issues.

    Let’s see html5Widgets give IE some oninput goodness

    In order to do this, I used ideas from Daniel Friesen’s blog post, A HTML5 Browser Maze, Oninput Support (thanks to Paul Irish for pointing me into that direction). The result works rather well. Note that I did not fix the IE9 “cannot use .value, must use .innerHTML” bug described earlier. It’s a little bit more verbose, but for now it’s what a developer needs to do for true cross-browser oninput support. I have also kept onforminput support in the code, just in case anyone has used it in the past (e.g. me). To ensure the best experience, I wouldn’t use both events on the same page.

    Note also I did not add support for oninput in any element other than form. I may do this after further research in the way it supported across browsers (for example, Firefox has an issue with oninput being used on textarea elements).

    Download

    Download the latest version of html5Forms.js, which includes html5Widgets with oninput support, from github.

    Further Reading

    Tags: Events · Events · Forms · HTML · HTML5 · JavaScript · Polyfills · Uncategorized · , , , , , , , ,

    2 responses so far ↓
    Give Feedback

    Don't be shy! Give feedback and join the discussion.

    Please Note: If you are asking for help using the information on this page or if you are reporting a bug with the code featured here, please include a URL that shows the problem you are experiencing along with the browser/version number/operating system combination where the issue manifests itself. Without this information, I may not be able to respond.

    An orange star denotes a required field.