Dynamic Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nitestarz
    New Member
    • Jul 2007
    • 24

    Dynamic Form

    I have a button that when clicked uses the below funciton to add an input box to the page. How can I get it to show a label before the input box:

    Name: input box goes here

    function add_student(){

    var StudentFirstnam e=document.crea teElement('inpu t');
    StudentFirstnam e.type='text';
    StudentFirstnam e.size='45';
    StudentFirstnam e.name='Student 2_Firstname';

    document.forms. Registration.ap pendChild(Stude ntFirstname);

    }
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    i guess you always know how to do that ;) ...

    1. create a span-Element
    2. create a textNode with createTextNode( 'your_label')
    3. appendChild the textnode to the span
    4. create your input-Node
    5. appendChild the input-node to the span
    6. appendChild the span to your form

    kind regards

    Comment

    • Nitestarz
      New Member
      • Jul 2007
      • 24

      #3
      thank you very much! :-)

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        your are always welcome ... ;) come back when you have more questions

        kind regards

        Comment

        • Nitestarz
          New Member
          • Jul 2007
          • 24

          #5
          I am trying to stylize a line. What did I do wrong? It doesnt want to work! I have:

          var student_text = document.create TextNode("Stude nt Information");
          student_text.st yle.color = "white";
          student_text.st yle.backgroundC olor = "567297";
          student_text.st yle.fontWeight = "bold" ;
          student_text.st yle.padding = "2px";
          student_text.st yle.width = "100";

          document.forms. Registration.ap pendChild(stude nt_text);

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            hmmm ... i think you cannot style a textnode ... only an html-element ... so you need to style the span ... probably you need a second span around your textNode

            Comment

            • Nitestarz
              New Member
              • Jul 2007
              • 24

              #7
              how do you apply a span around a textnode?

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                createElement one ;) and then appendChild the textnode to it ... you may imagine everything as a tree ... 'every document is a tree' ... so its always the same procedure to create and append nodes ... append makes a child of the node where the node is appended ...

                Comment

                • Nitestarz
                  New Member
                  • Jul 2007
                  • 24

                  #9
                  this is where my head starts to spin! :-)

                  var span_text = document.create Element('span') ;
                  var span_text.style .color = "white";
                  var span_text.style .backgroundColo r = "567297";
                  var span_text.style .fontWeight = "bold" ;
                  var span_text.style .padding = "2px";
                  var span_text.style .width = "100";

                  document.firstn ame_text.append Child(span_text );

                  is this right?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    i think thats right ... to that span-node you have to add the textnode ... when its not working please post the code for the entire creation of your nodes ... and add the code-tags to it ... it simplifies reading of the code ...

                    kind regrads

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      Originally posted by Nitestarz
                      this is where my head starts to spin! :-)

                      var span_text = document.create Element('span') ;
                      var span_text.style .color = "white";
                      var span_text.style .backgroundColo r = "567297";
                      var span_text.style .fontWeight = "bold" ;
                      var span_text.style .padding = "2px";
                      var span_text.style .width = "100";

                      document.firstn ame_text.append Child(span_text );

                      is this right?
                      aaarg ;)) ... no ... don't do the declaration in every line ... remove the var-keyword except of the first line ;)

                      Comment

                      • Nitestarz
                        New Member
                        • Jul 2007
                        • 24

                        #12
                        here is the javascript:

                        [CODE=javascript]function add_student(){

                        //// Create Line breaks ////
                        var linebreak0=docu ment.createElem ent("p");

                        //// Create text ////
                        var firstname_text = document.create TextNode("First Name: ");

                        //// Create input boxes ////
                        var StudentFirstnam e=document.crea teElement('inpu t') ;
                        StudentFirstnam e.type='text';
                        StudentFirstnam e.size='45';
                        StudentFirstnam e.name='Student 2_Firstname';

                        //// Create span ////
                        var span_text = document.create Element('span') ;
                        var span_text.style .color = "white";
                        var span_text.style .backgroundColo r = "567297";
                        var span_text.style .fontWeight = "bold" ;
                        var span_text.style .padding = "2px";
                        var span_text.style .width = "100";

                        //// Append ////

                        document.forms. Registration.ap pendChild(lineb reak0);
                        document.firstn ame_text.append Child(span_text );
                        document.forms. Registration.ap pendChild(first name_text);
                        document.forms. Registration.ap pendChild(Stude ntFirstname);

                        }
                        [/CODE]
                        here is the html:
                        [HTML]<input name="add_stude nts" type="button" class="submit" onClick="add_st udent()" value="Add More Students">[/HTML]
                        Last edited by acoder; Jul 17 '07, 11:18 AM. Reason: Code in tags

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          notice the every-time redeclaration of var span_text ... remove it except of the first one ...

                          Comment

                          • Nitestarz
                            New Member
                            • Jul 2007
                            • 24

                            #14
                            still doesnt want to work!

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by Nitestarz
                              still doesnt want to work!
                              Are you sure you have it like this:
                              [CODE=javascript]//// Create span ////
                              var span_text = document.create Element('span') ;
                              span_text.style .color = "white";
                              span_text.style .backgroundColo r = "#567297";
                              span_text.style .fontWeight = "bold" ;
                              span_text.style .padding = "2px";
                              span_text.style .width = "100px";[/CODE]

                              Comment

                              Working...