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,290
    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 08: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,290
    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,290
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.48770 seconds
  • Memory Usage 2,906KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates