Get Label Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Label

Introduction

The Label object represents an HTML <label> element.

You can access a <label> element by using getElementById():

Demo Code

ResultView the demo 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()">get the id of the element the label is bound to</button>

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

<script>
function myFunction() {/*  w ww.j  a  va 2s.c om*/
    var x = document.getElementById("myLabel").htmlFor;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials