www.webdeveloper.com
Results 1 to 6 of 6

Thread: delete controls

  1. #1
    Join Date
    Jan 2012
    Posts
    3

    delete controls

    Hi everyone,

    I have this:

    Code:
    <HTML>
    <HEAD>
    <TITLE>Dynamically add controls in html Form using JavaScript</TITLE>
    
    <SCRIPT language="javascript">
    var arrinput = new Array(0);
    var arrinputValue = new Array(0);
    
    
    //Begin add elements:
    function add(type) {
    
    
    //var counter = 0; 
        //Create an input type dynamically.
    	
    	//var i;
    	
    	//for(i = 0; i < 1; i++)
    	//{
    	
        
     var element = document.createElement("input");
        //Assign different attributes to the element.
        element.setAttribute("type", type);
        element.setAttribute("value", type);
        element.setAttribute("name", type);
     
        var foo = document.getElementById("fooBar");
     
        //Append the element in page (in span).
        foo.appendChild(element);
    		
    	//if(i > 3 )
    	//alert('U cant add anymore controls');
    	//}
    	
    	//foo.removeChild(element);
     
    }// End add function
    
    //Begin function delete
    function delete(type)
    {
    var i;
    	
    	if(document == element)
    	{
    	
    	var x;
    	x = document.getElementById("fooBar");
    	if (x.innerHTML){ x.innerHTML = '';}
    	x.parentNode.removeChild(x);
    		
    	//var d = document.getElementById("fooBar");
    	//var olddiv = document.getElementById("foobar");
    	//d.removeChild(type);
    	}
    	
    }
    // end function delete
    
    
    </SCRIPT>
    </HEAD>
    <BODY>
    <script type="text/javascript" src="formbuilder/javascript/manageControl.js"></script> 
    <FORM>
    <H2>Here u can add controls</H2>
    Select the element and hit Add to add it in form.
    <BR/>
    <SELECT name="element">
        <OPTION value="button">Button</OPTION>
        <OPTION value="text">Textbox</OPTION>
        <OPTION value="radio">Radio</OPTION>
    	<OPTION value ="label">Label</OPTION>
    	<OPTION value ="checkbox">Checkbox</OPTION>
    	<OPTION value ="dropdownlist">DropDownList</OPTION>
    	
    </SELECT>
     
    <INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/> 
    <INPUT type="button" value ="Delete" onclick="delete(document.forms[0].element.value)"/>
    <span id="fooBar">&nbsp;</span> 
    </FORM>
    </BODY>
    </HTML>
    I can add controls, but how to delete them?

    THX

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,265
    See the removeChild() method ... Since it would be useless to display the new elements in a new form and to list its. Good luck !
    Last edited by 007Julien; 01-19-2012 at 07:01 AM.

  3. #3
    Join Date
    Jan 2012
    Posts
    3
    That means?

    Can u give me the correction?

    THX

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,265
    At first, i propose to distinguish to part : the form to build and the tools to carry out this task.

    Secondly you have to distinguish the different elements to build (now only input with different types - label or dropdownlist are not types but others elements which will be probably after...).

    Thirdly, it could be useful to give this elements a name. A prompt could allow the user to enter this name....

    Besides, when you create a new element, you can store the new object (it type, name...) in an array and full a second dropdownlist describing the existing elements - see changing select element content on the fly). Then it would be possible to select the element to remove...

  5. #5
    Join Date
    Jan 2012
    Posts
    3
    I have it now like this:

    Code:
    <HTML>
    <HEAD>
        <TITLE> Add/Remove dynamic controls in HTML table </TITLE>
        <SCRIPT language="javascript">
            function addRow(tableID) {
     
                var table = document.getElementById(tableID);
     
                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);
     
                var cell1 = row.insertCell(0);
                var element1 = document.createElement("input");
                element1.type = "checkbox";
                cell1.appendChild(element1);
     
                var cell2 = row.insertCell(1);
                cell2.innerHTML = rowCount + 1;
     
                var cell3 = row.insertCell(2);
    			//cell3.document.getElementById("tableID");
                var element2 = document.createElement("tableID");
    			//element2 = document.setAtribute("button");
    			//element2.setAtribute("tableID",tableID);
    			//element2.setAtribute("value",tableID);
    			//element2.setAtribute("name",tableID);
    			element2.type= "button";
    			cell3.appendChild(element2);
    			
                //element2.type = "button";
                cell3.appendChild(element2);
     
            }
     
            function deleteRow(tableID) {
                try {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;
     
                for(var i=0; i<rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if(null != chkbox && true == chkbox.checked) {
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
     
                }
                }catch(e) {
                    alert(e);
                }
            } 
        </SCRIPT>
    </HEAD>
    <BODY>
    
    <BR/>
    <SELECT name="element">
        <OPTION value="button">Button</OPTION>
        <OPTION value="text"> Textbox</OPTION>
        <OPTION value="radio">Radio</OPTION>
    	<OPTION value ="label">Label</OPTION>
    	<OPTION value ="checkbox">Checkbox</OPTION>
    	<OPTION value ="dropdownlist">DropDownList</OPTION>	
    </SELECT>
     
        <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
     
        <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
    	<span id ="tableID">&nbsp;</span>
        <TABLE id="dataTable" width="350px" border="1">
            <TR>
                <TD><INPUT type="checkbox" name="chk"/></TD>
                <TD> 1 </TD>
                <TD> <INPUT type="text" /> </TD>
            </TR>
        </TABLE>
     
    </BODY>
    </HTML>
    U can select different control types.

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,265
    I thought something like this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Build form</title>
    <style type="text/css">
    body {margin:0;padding:20px;text-align:center;}
    #pge {display:block;width:500px;margin:0 auto;}
    form {border:1px solid gray;}
    #tls{border:1px solid yellow;}
    p{margin:0;text-align:left;}
    </style>
    <script type="text/javascript">
    var frmEls=[];
    function add(){var tlm,ntr,typ,elm,nme,frmObj,nmb,opt;
    	tlm=document.getElementById('slc').value;
    	if (!tlm || tlm=='undefined') {alert('No element is selected !');return}
    	ntr=(tlm=='select')?tlm:'input';
    	typ=(ntr=='input')?tlm:'';
    	alert(ntr+' '+typ)
    	frmObj=document.getElementById('frm');
    	elm=document.createElement(ntr);if (typ) elm.type=typ;
    	if (nme=prompt("Enter the name of this element ")) {elm.name=nme;}
    	frmObj.appendChild(elm);
    	nmb=frmEls.length;
    	frmEls[nmb]=elm;
    	opt=document.getElementById('eel');
    	opt.options[opt.options.length]=new Option(ntr+' '+nme+' '+typ,nmb,false,true);
    	alert(frmObj.innerHTML);
    }
    function del(){var frmObj;
    	frmObj=document.getElementById('frm');
    	dltElm=document.getElementById('eel');
    	if (!confirm("Delete "+dltElm.options[dltElm.selectedIndex].text+" ? "))return;
    	frmObj.removeChild(frmEls[dltElm.value]);
    	frmEls.splice(dltElm.value,1);
    	dltElm.remove(dltElm.selectedIndex); 
    	alert(frmObj.innerHTML);
    }
    </script>
        
    </head>
    <body>
    <div id="pge">
    <form id="frm">
    	<p><b>The future from</b></p>
    </form>
    <p>&nbsp;</p>
    	<div id="tls"><p><i>The tools</i></p>
    		<p>&nbsp;</p>
    		<p><select id="slc">
    			<option value="text">Input - textbox</option>
        	<option value="button">Input - button</option>
        	<option value="radio">Input - radio</option>
        	<option value="checkbox">Input - checkbox</option>
        	<option value="select">Drop-down list</option>
    		</select> to <input type="button" value="Add" onclick="add()"></p>
    	<p>&nbsp;</p>	
    	<p><select id="eel"></select> to <input type="button" value="Delete" onclick="del()"></p>
    	<p>&nbsp;</p>
    	</div>
    </div>
    </body>
    </html>
    It's a little more complex with tables and all others elements...

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