13

I have an html form. When you click the button, a javascript function adds a new field. I'm trying to have the function also add a 'label' for the field as well.

I've tried using document.createElement("LABEL"), but that doesn't let me change the innerHtml (or maybe I'm doing it wrong..), nor add a closing

Here is my code. Thanks! var instance = 2;

        function newTextBox(element)
        {       
            instance++; 
            // Create new input field
            var newInput = document.createElement("INPUT");
            newInput.id = "text" + instance;
            newInput.name = "text" + instance;
            newInput.type = "text";
            instance++; 

            document.body.insertBefore(document.createElement("BR"), element);
            document.body.insertBefore(newInput, element);

        }
    </script>
</head>


<body>
    <LABEL for="text1">First name: </LABEL>
    <input id="text1" type="text" name="text1">
    <LABEL for="text2">Last name: </LABEL>
    <input id="text2" type="text" name="text2">



    <input type="button" id="btnAdd" value="New text box" onclick="newTextBox(this);" />
</body>

3
  • post your code where you tried doing a document.createElement('LABEL') so we can debug it. Commented Dec 23, 2009 at 6:06
  • sorry, deleted it because I thought it was useless: var label = document.createElement("LABEL"); label.htmlFor = newInput.id; label.value = 'First Name'; Commented Dec 23, 2009 at 6:07
  • There's no such thing as label.value. Commented Dec 23, 2009 at 14:21

3 Answers 3

11
   function newTextBox(element)
            {               
                    instance++; 
                    // Create new input field
                    var newInput = document.createElement("INPUT");
                    newInput.id = "text" + instance;
                    newInput.name = "text" + instance;
                    newInput.type = "text";

                    var label = document.createElement("Label");

                    label.htmlFor = "text" + instance;
                    label.innerHTML="Hello";
                    instance++; 

                    document.body.insertBefore(document.createElement("BR"), element);
                    document.body.insertBefore(newInput,element);
                    document.body.insertBefore(label, newInput);

Note that for attribute of the label tag, corresponds to the property htmlFor fo the label object in javascript

Sign up to request clarification or add additional context in comments.

2 Comments

You cannot use label.for. The answer from Proto Bassi is correct.
Actually. Both these answers are wrong in some ways. They both advocate using innerHTML which is bad but more important, the 'for' is set using label.htmlFor
9

The example from Vincent does not work.

Try this:

var newlabel = document.createElement("Label");
    newlabel.setAttribute("for",id_from_input);
    newlabel.innerHTML = "Here goes the text";
parentDiv.appendChild(newlabel);

3 Comments

This is the correct answer. The attribute for must be set using setAttribute.
-1 for the suggestion of dom creation and then innerHTML usage instead of document.createTextNode
@Rafe, please give an example on how you will set the type to "Label" and how you are going to set an attribute on a TextNode. I don't thing your comment is anyhow helpful.
0
<div id="somediv">
 some div
</div>

<script>

var instance = 0;

function newTextBox(element){               

instance++; 
// Create new input field
var newInput = document.createElement("INPUT");
newInput.id = "text" + instance;
newInput.name = "text" + instance;
newInput.type = "text";

var newlabel = document.createElement("Label");
newlabel.setAttribute("for","text" + instance);
newlabel.innerHTML = "Here goes the text";
document.getElementById("somediv").appendChild(newlabel);
document.getElementById("somediv").appendChild(newInput);
}

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.