Drew Noakes
Drew Noakes

Reputation: 310977

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

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

Upvotes: 121

Views: 66311

Answers (1)

Drew Noakes
Drew Noakes

Reputation: 310977

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';

Upvotes: 207

Related Questions