Javascript DOM HTML Label Object create

Introduction

We can create a <label> element via the document.createElement() method:

var x = document.createElement("LABEL");

Click the button to create a LABEL element.

View 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  ww  w.j a v  a 2s  .c o m
<button onclick="myFunction()">Test</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>



PreviousNext

Related