Thursday, December 08, 2005

Setting Name for controls created using Javascript

when you create input controls dynamically using javascript like
var inputValue = document.createElement('input');
and try to set the id using
inputValue.setAttribute('id', 'controlName');
works fine, creates and control added is <input id=controlName>
but when you try to set the name attribute, does not reflect in the control, reason being IE does not allow you to change the name of the control, after it is created.
Work around for this is
var inputValue = document.createElement('<input name="controlName">');
this would create the control with the name element.

If asked why should you go for this work around, in postback when you try to use Request.Form to read the dynamically created controls, would work only if the name attribute is added.