The Linux Page

Javascript under Internet Explorer

Lately, I have been working on a website that has to work on all mordern browsers (in 2007). And I have had problems with Internet Explorer and Safari. Safari is more annoying because there are less hints on the Internet, but the solutions are still quite easy to find (it's not like it's all broken or something.)

Now, I have had many problems with Internet Explorer, but this one is quite interesting. When you need to dynamically create an element, you need to do something like document.createElement('input');. That works great. Once your element is created, you probably need to setup a few attributes. Note that for IE, you need to use 'className' instead of 'class' if you want to change the class. Noticed the capital N? Later I wanted to limit the number of characters typed to 3. So I did a new_input.setAttribute('maxlength', 3);. That was working for every single browser except IE. The fact is, I had to write maxLength. Not MAXLENGTH, not maxlength, but maxLength. The HTML documentation says that attributes are case insensitive, doesn't it?!

	new_input = document.createElement('input');
	new_input.setAttribute('id', 'my_input');
	new_input.setAttribute('maxlength', 'my_input');	// won't work in IE
	new_input.setAttribute('maxLength', 'my_input');	// works in IE and others
	new_input.setAttribute('name', 'Beautiful');

The current reference to the input (and other) tags. The following link is useful to make sure you get the case right. He Oui!

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input