www.webdeveloper.com
Results 1 to 6 of 6

Thread: dynamic form2

  1. #1
    Join Date
    Feb 2012
    Posts
    29

    dynamic form2

    I have this code:
    Code:
    <script type="text/javascript">
    function addFields(field,area,count,limit) 
    {
    	if(count > limit && limit > 0) return;
    	switch (field)
    		case "download":
    			for (var i = 0; i < count; i++) 
    			{
    				var dd = document.createElement("dd");
    				var input = document.createElement("input");
    				input.type="text";
    				input.name="download"+i;
    				input.id="download"+i;
    				dd.appendChild(input);
    				area.appendChild(dd);
    			}
    		break;
    } 
     </script>
    Code:
    		<dl>
    			<dt><label for="modauthor">Download Fields:</label></dt>
    			<dd><input type="text" name="dnum" id="dnum" size="3" maxlength="3" value="0" class="inputbox autowidth" /><input type="button" value="Set" onclick="addFields('download',document.getElementById(download_area),document.getElementById("dnum").value,0);" /></dd>
    		</dl>
    Code:
    		<dl id="download_area">
    		</dl>
    and I'm having some trouble with it.

    1. The set button isn't calling the addFields function

    2. I need this to either check how many fields there are already and either add to them or subtract from them. OR if it is easier, just remove all the fields and then re-add the correct amount. How would I do that?

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    I went for the easier route cause Im lazy right now :P

    Code:
    <script type="text/javascript">
    
    	function addFields(field, area, count, limit)
    	{
    		if(count > limit && limit > 0) return;
    		switch (field) {
    			case "download":
    				area.innerHTML = '';
    				for (var i = 0; i < count; i++) {
    					var dd     = document.createElement("dd");
    					var input  = document.createElement("input");
    					input.type = "text";
    					input.name = "download" + i;
    					input.id   = "download" + i;
    					dd.appendChild(input);
    					area.appendChild(dd);
    				}
    				break;
    		}
    	}
    
     </script>
    
    <dl>
    	<dt><label for="modauthor">Download Fields:</label></dt>
    	<dd><input type="text" name="dnum" id="dnum" size="3" maxlength="3" value="0" class="inputbox autowidth" /><input type="button" value="Set" onclick="addFields('download',document.getElementById('download_area'),document.getElementById('dnum').value,0);" /></dd>
    </dl>
    
    <dl id="download_area">
    </dl>

  3. #3
    Join Date
    Feb 2012
    Posts
    29
    Thanks, but the set button still never calls the function, what's wrong here?

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    You did use the button I submitted as well right? Cause there were quoting errors in your original one:

    HTML Code:
    <dl>
    	<dt><label for="modauthor">Download Fields:</label></dt>
    	<dd><input type="text" name="dnum" id="dnum" size="3" maxlength="3" value="0" class="inputbox autowidth" /><input type="button" value="Set" onclick="addFields('download',document.getElementById('download_area'),document.getElementById('dnum').value,0);" /></dd>
    </dl>

  5. #5
    Join Date
    Feb 2012
    Posts
    29
    Quote Originally Posted by bionoid View Post
    You did use the button I submitted as well right? Cause there were quoting errors in your original one:

    HTML Code:
    <dl>
    	<dt><label for="modauthor">Download Fields:</label></dt>
    	<dd><input type="text" name="dnum" id="dnum" size="3" maxlength="3" value="0" class="inputbox autowidth" /><input type="button" value="Set" onclick="addFields('download',document.getElementById('download_area'),document.getElementById('dnum').value,0);" /></dd>
    </dl>
    Yes, i'm using your revised code, when I press the set button nothing happens and in the debugger the function is never called.

  6. #6
    Join Date
    Nov 2010
    Posts
    1,102
    Quote Originally Posted by grimshad View Post
    Yes, i'm using your revised code, when I press the set button nothing happens and in the debugger the function is never called.
    works fine for me. Maybe you should work on your copy and pasting before moving on to javascript, grimshad? This is the second time in two days that you've had this problem

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles