1. Computing

Naming DOM Created Fields and Internet Explorer

Join the Discussion

Questions? Comments?

Among its many quirks and bugs, Internet Explorer omits support for one of the most important attributes that you will need to add in your code to be able to update the web page to include form content. Internet Explorer does not allow you to add a name attribute to any of your form fields after the tag itself is created.

All other browsers except for Internet Explorer provide two ways to name a form field. These are:

newNode.name = myname;
newNode.setAttribute('name',myname);

While Internet Explorer doesn't stop your Javascript continuing to run when it reaches one of those statements it wont add the name attribute to your tag either which can easily leave you with a non-functioning form.

Internet Explorer does provide a way to add a name to a tag but you have to do it when actually creating the tag rather than afterwards. As well as supporting the standard createElement command where you just specify the type of element you want to create, Internet Explorer also allows you a second way to use that command where you can specify as many attributes as you like within the command itself as long as you define the content as a valid element. We can therefore define an input field and give it a name in Internet Explorer like this:

var newNode =
document.createElement("<input name='"+myname+"'>");

Note that we can do the same to add a name to any tag but if the tag is one that needs to be closed (eg. a paragraph) then you also need to include the closing tag (but not the content) in the command as well, eg.

var newNode = document.createElement(
"<select name='"+myname+"'></select>");

Unfortunately that code will cause the Javascript to malfunction in most other browsers as they don't understand Internet Explorer's totally weird and completely non-standard way of doing things. All is not lost however because Internet Explorer also supports special conditional commands that allow us to confirnm whether we are running JScript (and therefore Internet Explorer) or Javascript in a real web browser. As these conditional commands can be placed inside comments so that other browsers will ignore them we can therefore set up DOM commands to successfully create and name tags. For example to create an input element and give it the name stored in a variable called myname we would use the following code:

/*@cc_on @if (@_jscript)
var newNode =
document.createElement("<input name='"+myname+"'>");
@else */
var newNode = document.createElement("input");
newNode.name = myname;
/* @end @*/

We can of course then add as many other attributes as the field requires following this code and have the same code apply to all web browsers as Internet Explorer only dislikes following the standards for the name attribute.

Related Video
How to Modify Text Size in Internet Explorer
Computer Cable Ports 101

©2013 About.com. All rights reserved.