Label htmlFor Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Label

Description

The htmlFor property sets or gets the for attribute of a label, which sets which form element a label is bound to.

Set the htmlFor property with the following Values

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

Return Value

A String, representing the id of the element the label is bound to

The following code shows how to return the value of the for attribute of a label:

Demo Code

ResultView the demo 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>/*  ww  w. j  av  a2 s.com*/

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

Related Tutorials