IE and DOM buttons – The correct way

by

in

I have had so many problems creating buttons in IE using the dom that I had gone to just using images as buttons and adding them. Turns out there is one simple thing you can do to get it to work. Create the button, then add it to the page…

Here is what I was doing:

var button = item.appendChild(document.createElement('input'));
button.type='button';
button.value='THIS IS A BUTTON';

This code always threw an error, however if you do it this way (creating the button, and then adding it) it works fine:

var button = document.createElement('input');
button.type='button';
button.value='THIS IS A BUTTON';
button = item.appendChild(button);

Have fun dynamically creating forms that will work in most browsers!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *