Results 1 to 5 of 5

Thread: Dynamically add controls to a page

  1. #1

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Dynamically add controls to a page

    I want to add controls to a page on the click of a button.

    The page needs to have multiple lines each containing 2 text boxes and a select with a new row being added each time a button is clicked.

    I could do it by reloading the page but is it possible using just javascript?
    Mark
    -------------------

  2. #2
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    You can use createElement. Heres an example

    Code:
    <html>
    <head>
    <title>Create Element Example</title>
    <style type="text/css">
    <!--
    body {font-family:arial; font-size:8pt;)
    //-->
    </style>
    
    <script language="JavaScript" type="text/javascript">
    <!--
    function drawNewElement(objectID){
      var aElement=document.createElement(objectID);
      document.getElementById('form1').appendChild(aElement);
    }
    //-->
    </script>
    
    </head>
    <body>
    <form name="form1" id="form1">
      <label>This is a form</label><br>
      <label>Create New Element (HTML Code for the element you wish to create E.g. &lt;input type="button"&gt;</label><input type="text" name="obj">
      <input type="button" value="Create" onclick="drawNewElement(document.forms('form1').obj.value)"><br>
    </form>
    </body>
    </html>
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  3. #3

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Thanks for that Lee

    Having given it some thought, the page is so complicated that I think I'll use the reloading method instead.


    Thanks
    Mark
    -------------------

  4. #4

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    According to the MSDN site I can
    You can also specify all the attributes inside the createElement method itself by using an HTML string for the method argument.
    Any ideas why the options aren't showing in the select?

    Code:
    <html>
    <head>
    
    
    <script language="JavaScript" type="text/javascript">
    <!--
    var f;
    function init()
    {
    f= document.getElementById('form1')
    }
    function newRow(){
      var aElement=document.createElement("<input name='t1'>")
      var oOption;
      f.appendChild(aElement);
      
      aElement=document.createElement("<SELECT name='s' style='WIDTH: 136px' id=select1><option selected value=1>Oprion 1</option><option value=2>Option 2</option></SELECT>");
      f.appendChild(aElement);
      
      aElement=document.createElement("<input name='t2'>")
      f.appendChild(aElement);
      
      aElement=document.createElement('br');
    	f.appendChild(aElement);
    
    
    }
    //-->
    </script>
    
    </head>
    <body onLoad='init()'>
    <form name="form1" id="form1">
    <input name="txt1" >
      <input type="button" value="Add Row" onclick="newRow()"><br>
    
    
    </form>
    
    
    </body>
    </html>
    Mark
    -------------------

  5. #5

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Sussed it now
    Mark
    -------------------

Posting Permissions

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



Click Here to Expand Forum to Full Width