1. ホーム
  2. javascript

[解決済み] Setting the HTML <label> 'for' attribute in JavaScript

2022-12-04 21:36:42

Question

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

How to solved?

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