View Full Version : Add element script

01-17-2009, 06:30 PM
Hi guys i have this script to add a textbox at runtime. I wanted to limit the number of textboxes the user could throw out so i added a counter and limit to the script. Only now it doesn't work. Any suggestions why?

Here's the code

var counter = 1;
var limit = 3;
function addEvent() {
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
} else {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value -1)+ 2;
numi.value = num;
var divIdName = "skill"+num;
var newdiv = document.createElement('div');
newdiv.innerHTML = "<input type='text' name='myInputs[]'><a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove</a>";

Philip M
01-17-2009, 06:50 PM
I don't see anything wrong and it works when I test it in IE.

Presumably var counter = 1;
should be var counter = 0;
if three additional textboxes are allowed.

"That which seems the height of absurdity in one generation often becomes the height of wisdom in the next". - John Stuart Mill.

01-17-2009, 07:12 PM
I have an alternate question now. If i populate an array with php and push it to javascript. How can i replace my input box with a select box?