Results 1 to 2 of 2

Thread: add another field

  1. #1
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add another field

    I am looking for some code reminiscent of gmail's "attach another file"

    What i need is a text input, and next to it, an "add another entry" link
    when it is pressed, another text input is displayed below with the "add another entry" link next to it.

    when the form is submitted, i would like all the results to be combined into one entry separated by colons.
    ...however, i am able to do the latter after the form is submitted



    EDIT: it would also be nice to have a "delete field" option (in case if "add another" was clicked too many times")
    Last edited by ches; 12-28-2006 at 03:37 PM.

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Forms Field Dynamically!!</title>
    <script type="text/javascript">
    function addField()
    {
    	var mainOb = document.getElementById('mainDiv');
    	var valOb = document.getElementById('val');
    	var num = (document.getElementById("val").value -1)+ 2;
    	valOb.value = num;
    	var newDivName = "my"+num+"Div";
    	var newDiv = document.createElement('div');
    	newDiv.setAttribute("id",newDivName);
    	newDiv.innerHTML = "<input type='text' name='"+newDivName+"'>&nbsp;<a href='#' onclick='removeField(\""+newDivName+"\");'>Remove Field</a>";
    	mainOb.appendChild(newDiv);
    }
    
    function removeField(divNam)
    {
    	var o = document.getElementById('mainDiv');
    	var oldDiv = document.getElementById(divNam);
    	o.removeChild(oldDiv);
    }
    </script>
    </head>
    
    <body>
    <form>
    <input type="hidden" value="0" id="val" />
    <p><a href="#" onclick="addField();">Add Some Elements</a></p>
    <div id="mainDiv"> </div>
    </form>
    </body>
    </html>
    The above code does the dynamic insertion of form field, in this case text box.

Bookmarks

Posting Permissions

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