Javascript DOM HTML Label htmlFor Property set

Introduction

Change the value of the for attribute of a label:

document.getElementById("myLabel").htmlFor = "newValue";

Click button to change 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 w  w. j  a va 2  s  .co m
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  document.getElementById("myLabel").htmlFor = "newValue";
  document.getElementById("demo").innerHTML = "The value of the for attribute was changed.";
}
</script>

</body>
</html>



PreviousNext

Related