0

I want generate textboxes dynamically according to user input. If user enter the integer value in the textbox, if he/she enter 5 i want generate 5 textboxes. This code is working in Firefox but not working in IE and Netscape; please help me how to do this or point out any other mistake in this code to me. Also, the technology we are using is Struts 2.
Please help me.

JavaScript code:

function generate()
{
  var tot = document.getElementById("totmob").value;
  var tbl = document.getElementById("sim");

for(var i =1;i<=tot;i++)
{
  tbl.innerHTML  = 'Mobile No'+i+' <input type="text"  size = "20" maxlength= "20" name= hoardingregister.mobileno> <br> \n';
}

HTML code:

<td>
<s:textfield id="totmob" label="Total Mobile Number"  />
<td>
<td>
<input type="button"  value="ADD" onclick="generate()"/>
</td>
</tr>
</table>
<div id="sim">
</div>
2
  • Your HTML code is not visible. Please change all your tags from <tag> to <tag> Commented Feb 12, 2009 at 7:35
  • a more specific title for your question would help others help you. also, try formatting your code. Commented Feb 12, 2009 at 7:36

6 Answers 6

2
for(var i =1;i<=tot;i++)
{
  tbl.innerHTML  = 'Mobile No'+i+' <input type="text"  size = "20" maxlength= "20" name= hoardingregister.mobileno> <br> \n';
}

should be

for(var i =1;i<=tot;i++)
{
  tbl.innerHTML  += 'Mobile No'+i+' <input type="text"  size = "20" maxlength= "20" name= hoardingregister.mobileno> <br> \n';
}

because you need to append to the inner HTML, rather than replace it.

You've also used a <td> instead of a </td>

Sign up to request clarification or add additional context in comments.

Comments

0

Can you try something else than InnerHtml - you can try this:

  • use GetElementByID to get the element you want to add text boxes to
  • remove any existing child items if required
  • Add textboxes as child items to the selected node

Comments

0

From your javascript code, it looks you should use following code:

tbl.innerHTML  +=

instead of

tbl.innerHTML  =

inside the for loop.

Comments

0

The code below works perfect for me in IE as well as Firefox. When you say It's nt working, what do you mean ?

function generate()
{
  var tot = document.getElementById("totmob").value;
var tbl = document.getElementById("sim");

for(var i =1;i<=tot;i++)
{
  tbl.innerHTML  = tbl.innerHTML  +'Mobile No'+i+' <input type="text"  size = "20" maxlength= "20" name= hoardingregister.mobileno> <br> \n';
}

}
</script>
<body>
<table>
<tr>
    <td>
        <span>Total Mobile Number  </span>
        <input type="text" id="totmob" />
    <td>
    <td>
    <input type="button"  value="ADD" onclick="generate()"/>
    </td>
</tr>
</table>
<div id="sim">
</div>
</body>
</html>

1 Comment

Naveen, Can you share what went wrong and what change fixed your problem?
0

add a unique identifier to the name of the input or a unique id to it.

like:

for(var i =1;i<=tot;i++)
{
  tbl.innerHTML  += 'Mobile No'+i+' <input type="text"  size = "20" maxlength= "20" name= hoardingregister.mobileno'+i+'> <br> \n';
}

or you put a id into it like:

id="input"+i

Comments

0

But if you are changing the value in the text box without refreshing the page more text boxes are adding after the previously added text box. But it is wrong whenever we change the value in the text box only tat number of new text boxes should be added instead of keeping the previous text boxes too. Can you please change the coding according to the conditions I said.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.