Setting the HTML <label> 'for' attribute in JavaScript

JavascriptDom

Javascript Problem Overview


How do you set the for attribute of an HTML <label> element in JavaScript, without using jQuery or any other library?

Javascript Solutions


Solution 1 - Javascript

Use the htmlFor attribute. I assume it has a slightly cryptic name because for is a keyword in JavaScript:

var label = document.createElement('label');
label.htmlFor = 'some-input-id';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrew NoakesView Question on Stackoverflow
Solution 1 - JavascriptDrew NoakesView Answer on Stackoverflow