Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Add element script

    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
    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.setAttribute("id",divIdName);
    	newdiv.innerHTML = "<input type='text' name='myInputs[]'><a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove</a>";
    	ni.appendChild(newdiv);
    	counter++;
      	}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •