views:

153

answers:

1

I am using the following JavaScript to add a number of inputs based on a number in a drop down menu. It works fine in firefox and chrome, but in IE7 the inputs all float left on a new line.

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var element = document.createElement("input");

//Assign different attributes to the element.
element.setAttribute("name", "value"+count);
element.setAttribute("id", "value"+count);
element.setAttribute("size", "60");
element.setAttribute("type", "text");
element.setAttribute("value", "");

//Append the element in page (in span).
foo.innerHTML+=('<li class="inputCount"><label for="value'+count+'">#'+count+'</label>');
foo.appendChild(element);
foo.innerHTML+=("</li>");
count += 1;
}

}
A: 

I get the same bug in Firefox: you're adding the input as a child of listInput2, not of the li.

Here's a piece of code that works:

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var input = "<input name='value"+count+"' id='value"+count+"' size='60' type='text' value=''>";

//Append the element in page (in span).
foo.innerHTML+='<li class="inputCount"><label for="value'+count+'">#'+count+'</label>'+input+'</li>';
count += 1;
}

}
Jerome