Change button ‘type’ for dynamic buttons in IE using Javascript
Internet Explorer / Firefox Issues, Technology November 21st, 2010Recently I was working on a piece of code where the requirement was to create buttons dynamically on click of another link/button. The code was:
var button1=document.createElement(’INPUT’);
button1.name =’button1_name’;
button1.setAttribute(”type”,”button”);
button1.setAttribute(”id”,”button1_id”);
button1.setAttribute(”value”,”Submit”);
The above code was working perfect in Firefox. However, in IE 6, this code did not create a button. It simply didn’t work! While trying to find a solution, I came across a link that said while creating dynamic buttons through Javascript, the ‘type’ attribute can’t be set dynamically using setAttribute() in IE 6. What needed in IE 6 was:
var button1=document.createElement(’<input type=”button” name=”button1_name” />’);
The above code will also work for Firefox, thus to make it cross-browser, had to hard-code the tag as written above.
Related posts:





Recent Comments