Adding a label to a TextNode

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

    Adding a label to a TextNode

    ok, I have a TextNode:
    [code=javascript]
    var firstname_text = document.create TextNode("First Name: ");
    [/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    How would I be able to add a <label> tag to it so that when it prints out it is wrapped in the <label> tag?

    Thanks!!!!
    Last edited by pbmods; Jul 19 '07, 03:59 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Nitestarz.

    First, you'd need to create the label, then append your text node to it.
    [code=javascript]
    var theLabel = document.create Element('label' );
    var firstname_text = document.create TextNode("First Name: ");
    theLabal.append Child(firstname _text);
    [/code]

    Comment

    • Nitestarz
      New Member
      • Jul 2007
      • 24

      #3
      duhh!!! thank you..my brain is slowww today

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hi ...

        you have to create the label-node and then appendChild the textNode to it ...

        your document-node is to imagine like this after you've done it:

        Code:
        <label>
            |_ <textnode>
        after that ... append the label-node to your document wherever you want ...

        kind regards

        ps: ahh ... i see you got help during typing my post ;))

        Comment

        • Nitestarz
          New Member
          • Jul 2007
          • 24

          #5
          Hmmm. It doesnt seem to be adding. Here is my whole code i am using:
          Code:
          function add_student(){
          
          ///Create Label///
          var label = document.createElement('label');
          
          //// Create Line breaks ////
          var linebreak=document.createElement("p");
          var linebreak0=document.createElement("p");
          
          //// Create text ////
          var header = document.createTextNode("Student Information");
          var firstname_text = document.createTextNode("First Name: ");
          
          //// Create input boxes ////
          var StudentFirstname=document.createElement('input') ;
          StudentFirstname.type='text';
          StudentFirstname.size='45';
          StudentFirstname.name='Student2_Firstname';
          
          //// Append ////
          document.forms.Registration.appendChild(header);
          document.forms.Registration.appendChild(linebreak0 );
          label.appendChild(firstname_text);
          document.forms.Registration.appendChild(firstname_text);
          document.forms.Registration.appendChild(StudentFirstname);
          }

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            where do you append the label? ;) ... instead of appending the textnode to the document (line 24), you have to append the label, that has the textnode appended already ... when you append it now, you append the 'label+textnode-construct' ... ok?

            kind regards

            Comment

            • Nitestarz
              New Member
              • Jul 2007
              • 24

              #7
              I think i am more confused now!

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                Originally posted by Nitestarz
                I think i am more confused now!
                replace line 24 with:

                [CODE=javascript]document.forms. Registration.ap pendChild(label );[/CODE]

                Comment

                • Nitestarz
                  New Member
                  • Jul 2007
                  • 24

                  #9
                  ahhh! i see how that works now! thank you :-)

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    no problem ;) ... come back when you have more questions ...

                    kind regards

                    Comment

                    Working...