/

HTML / CSS / JavaScript Tutorial

HTML Element: <label>

[this page | pdf | back links]

The HTML <label> element indicates a label for an <input> element. It does not appear special as far as the user is concerned, but does improve the usability of the <input> element, as it means that if the user clicks on the text within <label> element then it toggles the control.

 

The attributes it can take (other than HTML global attributes and HTML event attributes) include:

 

Attribute

Description

More

for

Specifies which form element(s) a label calculation is bound to

Here

form

Name of the form that element belongs to (this should be the id attribute of the element to which the label element relates, to bind the two together)

Here

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods, and additional properties with the same name and meaning as the attributes of the underlying HTML element referred to above (with the for property of the underlying element corresponding to the htmlFor property of the DOM object). It used to support the control property, but this was removed from the HTML specification in 2016.

 

The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using HTML:<br>
<label for="input1">Apple</label><input type="radio" id="input1"><br>

<br><br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var x1 = document.createElement("LABEL");
x1.setAttribute("for","input1");
x1.textContent = "Apple";
var x2 = document.createElement("INPUT");
x2.setAttribute("type","radio");
x2.setAttribute("id","input1");
document.getElementById("element").appendChild(x1);
document.getElementById("element").appendChild(x2);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile