25

I want to create label and check box dynamically. when i enter text in text box and give submit button,a check box and label with the text i entered in text box should be created..How to create ?

0

2 Answers 2

29

For the label try this:

var newlabel = document.createElement("Label");
newlabel.setAttribute("for",id_from_input);
newlabel.innerHTML = "Here goes the text";
parentDiv.appendChild(newlabel);
Sign up to request clarification or add additional context in comments.

Comments

17

I think you want this..

//html
<div id="container">
<input id="test" type="text"  >
<input value="add" type="button"  onClick="add()">
</div>

//js
<script>
var i=0;
function add(){    
    if (document.getElementById('test').value!='') 
    {   
        i++;  
        var title   =document.getElementById('test').value;
        var node = document.createElement('div');        
        node.innerHTML = '<input type="checkbox" id="check' + i + '" name="check' + i + '"><label for="check' + i + '">'+ title +'</label>';       
        document.getElementById('container').appendChild(node);    
    }
}
</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.