Javascript DOM HTML Label Object get

Introduction

The Label object represents an HTML <label> element.

We can access a <label> element via document.getElementById():

var x = document.getElementById("myLabel");

Click the button to get the id of the element the label is bound to.

View in separate window

<!DOCTYPE html>
<html>
<body>
<label id="myLabel" for="male">Male</label>
<input type="radio" name="sex" id="male" value="male"><br>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//w  w  w  .java 2  s. c o  m
  var x = document.getElementById("myLabel").htmlFor;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related