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?

+49
javascript dom
Apr 01 '13 at
source share
1 answer

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

 var label = document.createElement('label'); label.htmlFor = 'some-input-id'; 
+80
Apr 01 '13 at
source share



All Articles