Javascript DOM HTML Label htmlFor Property get

Introduction

Return the value of the for attribute of a label:

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

Click button to return the value of the for attribute of the label.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  <label id="myLabel" for="male">Male</label>
  <input type="radio" name="sex" id="male" value="male"><br>
</form>//  w  ww.j  a va2 s.  c  o  m
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.getElementById("myLabel").htmlFor;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The htmlFor property sets or gets the value of the for attribute of a label.

The for attribute specifies which form element a label is bound to.

The htmlFor property accepts and returns a String type value.

Value Description
idThe id of the element the label is bound to

The htmlFor property returns a String representing the id of the element the label is bound to.




PreviousNext

Related