Results 1 to 4 of 4

Thread: How to add forms Dynamically with JavaScript

  1. #1
    Join Date
    Feb 2009
    Posts
    62

    How to add forms Dynamically with JavaScript

    I am doing a form requesting the user to choose from a menu bar, a number of people and javascript I have to create textfield just how many the number who choose you.
    here we should able to choose the number of children and then are shown in the textfield so many children who are chosen to enter the age of each.
    I also call these form as "child1", "child2", "child3" ect ..

    how?

  2. #2
    Join Date
    Dec 2008
    Posts
    183

    Re: How to add forms Dynamically with JavaScript

    Creating the Form

    First we locate the paragraph, which is where we want to position the form. Then we create the form and the select. The next thing I do is to create the onClick event for the select.

    Code:
    mypara=document.getElementById("paraID");
    
        myform=document.createElement("form");
    
        myselect = document.createElement("select");
    
        //one way to write a function... you have to write it yourself!
    
        myOnChange = new Function("e", "location.href=myselect.options[myselect.selectedIndex].value");
    Here you will get the full information about how to create a form with the dynamic content.

  3. #3
    Join Date
    Apr 2008
    Posts
    193

    Re: How to add forms Dynamically with JavaScript

    Not all forms are meant to be static. Sometimes, you want to allow the users to add certain parts of the form as they need them. Here is a nice example of dynamically adding inputs to your form as users need them. In addition, an input limit has been implemented in the script and it is set to 3.

    Here I will explain it by the HTML coding:

    Code:
    <script src="/wp-includes/js/addInput.js" language="Javascript" type="text/javascript"></script>
    <form method="POST">
         <div id="dynamicInput">
              Entry 1<br><input type="text" name="myInputs[]">
         </div>
         <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
    </form>
    When the user clicks on the Add another text input button, the addInput JavaScript function is executed and given the dynamicInput argument which is the name of the div that contains all our text inputs. Here is what our JavaScript function looks like:

    Code:
    var counter = 1;
    var limit = 3;
    function addInput(divName){
         if (counter == limit)  {
              alert("You have reached the limit of adding " + counter + " inputs");
         }
         else {
              var newdiv = document.createElement('div');
              newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
              document.getElementById(divName).appendChild(newdiv);
              counter++;
         }
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    202

    Re: How to add forms Dynamically with JavaScript

    Your idea is slightly misaligned. with javascript, you need to create things as an object - create the 'div' object, then use appendChild method to attach it where you need it. Then, add an EventListener to your <select> box - that can call a function which looks up the value of the selected entry and deal with it as required.

Similar Threads

  1. Dynamically Loading External JavaScript and CSS Files
    By Adrina_g in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 11:36 AM
  2. How to calculate value dynamically in JavaScript?
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 11:01 PM
  3. How to Add and Remove HTML elements Dynamically In Javascript?
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 06:48 PM
  4. How to dynamically expand the javascript height?
    By Recko in forum Software Development
    Replies: 3
    Last Post: 09-07-2009, 08:19 PM
  5. Javascript to rewrite the function dynamically
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 18-06-2009, 10:19 AM

Tags for this Thread

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
  •  
Page generated in 1,713,908,939.76428 seconds with 17 queries