Internet Explorer and its buggy "change" event handling

The problem

I was recently working through some Internet Explorer issues with a web application I've been involved in developing, when I came across a strange little bug related to the "change" event. I found that I had to click a radio button twice, or click a radio button and then click elsewhere on the page in order for the event to be triggered. This obviously pointed towards the event triggering to be buggy in Internet Explorer.

The explanation

From QuirksMode:

IE fires the event when the checkbox or radio is blurred, and not when it is activated. This is a serious bug that requires the user to take another action and prevents a consistent cross-browser interface based on the change event on checkboxes and radios.

The solution?

I see there being 3 possible solutions:

  1. Use the "click" event rather than the "change" event.
  2. Use browser specific targeted script in the JavaScript functions where the "change" event is required.
  3. Use conditional comments to include IE specific JavaScript, and trigger blurring and then focusing the field.
The best solution right now seems to be to use the "click" event rather than "change" in these scenarios. This is certainly not an elegant solution and does indeed "break" the keyboard functionality since it is possible to change the value of an input field with the keyboard and hence not trigger a "change" event. I'd say the 3rd solution comes a close runner up, and the 2nd solution definitely being suboptimal, for the reason that it is difficult to consistently detect the browser using JavaScript.

I found it interesting, so I thought I'd share and maybe it will help someone! I'd love to hear your opinions on what you think the optimal solution is.

If you find my posts useful, you should check out my OnePage and follow me on Twitter.

Filed under  //   change   event   ie   javascript   jquery  

Comments [1]

IE's broken implementation of the DOM: where did the the name attribute go?

I hope to make this blog useful both from an entrepreneurial standpoint and a web development standpoint. Here is a more technical entry.

Problem

I just had to solve an issue I was having with input elements dynamically inserted via JavaScript and it took me an hour or so to find the solution, so I thought I'd post it here for the opportunity it may help someone else reach the solution faster.

The problem arose when I discovered that a tool I had developed which uses AJAX extensively to change the options available to the user depending on other options was not working in Internet Explorer (testing was on IE7).

It was not immediately obvious that it was IE's broken implementation of the DOM which was causing the issue. The first thing I discovered was that the serialize() function was not including elements. I then discovered that this was only when the elements had been inserted dynamically via JavaScript. I then happened to find a post by Aaron Gustafson on Easy! Reader entitled "Death to bad DOM implementations", which outlined the very issue.

The problem is that IE disallows changing of the name attribute of elements after the element has been created. Thus the usual method I use won't work:

 $(document.createElement('input')).attr("name", "email")); 

The reason is that the input element is created and then the name attribute is added once the element already exists. IE doesn't like this.

Solution

In the comments of Aaron's post, Chris Purcell has a very nice function to use which solves the problem:
document.createNamedElement = function(type, name) {
    var element;
    try {
        element = document.createElement('<'+type+' name="'+name+'">');
    } catch (e) { }
    if (!element || !element.name) { // Not in IE, then
        element = document.createElement(type)
        element.name = name;
    }
    return element;
}
So you can then simply do (for form elements):
 $(document.createNamedElement('input', 'email)); 
If that helps even just one person out there I'll be very happy. Also, documenting it reinforces it in my memory for the future.

If you find my posts useful, you should follow me on Twitter! :)
Filed under  //   attribute   broken   dom   element   form   ie   javascript   jquery   name   serialize  

Comments [0]

Problem with Flash appearing on top of Facebox

logo.png

Facebox is a modal window plugin for jQuery. It's fantastic, so give it a go.

The problem

I just had an issue with Flash appearing above a Facebox window in one of the projects I've been working on. Thought it may be useful to post the solution I found, since it wasn't an instant find for me.

The solution

When you embed flash on a page, you'll almost always have an <object> element and an <embed> element.

What you need to do is add <param name="wmode" value="transparent"></param> as a child element to the <object> element, and add wmode="transparent" as an attribute to the <embed> element.

That's it, fixed it nicely for me :) Happy coding!
Filed under  //   facebox   flash   javascript   jquery   problem   solution  

Comments [15]

About

Startup guy and full stack web developer. Most recently founded Buffer, also co-founded OnePage. Keen to learn and striving to do what I love. Say hello :)

I post my longer reflections over on my blog.

TwitterFacebookLaconi.ca/Identi.caLinkedInFriendfeedFlickrYoutubeVimeoDeliciousTumblr