Create a Label Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Label

Introduction

You can create a <label> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="/action_page.php">
  <input type="radio" name="sex" id="male" value="male">
</form>/*from  w ww.  j a  v  a  2  s.c  o  m*/

<button onclick="myFunction()">create a LABEL element</button>

<script>
function myFunction() {
    var x = document.createElement("LABEL");
    var t = document.createTextNode("Male");
    x.setAttribute("for", "male");
    x.appendChild(t);
    document.getElementById("myForm").insertBefore(x,document.getElementById("male"));
}
</script>

</body>
</html>

Related Tutorials