Javascript examples for DOM HTML Element:Label
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 |
---|---|
id | The id of the element the label is bound to |
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:
<!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>