开发者

Dynamically creating label tag with the "for" attribute

开发者 https://www.devze.com 2023-02-01 11:45 出处:网络
In HTML, you can assign the \"for\" attribute of a label tag so that when the user clicks the label, it selects the corresponding radio button:

In HTML, you can assign the "for" attribute of a label tag so that when the user clicks the label, it selects the corresponding radio button:

<input type="radio" name="group" value="1" id="radioButtonId" />
<label for="radioButtonId">label text</label>

There's problems when creating the label tag dynamically with javascript (specifically with Prototype JS framework). For is a reserved keyword for for loops. Prototype JS's d开发者_开发知识库ocumentation shows that className is the code word for the reserved keyword class, but it doesn't say what the code word for for is. What is it?

new Element(
 'label', {
  for: 'radioButtonId'
 }
).update('label text');


className is the standard DOM property corresponding to the class attribute; it's nothing to do with Prototype per se.

Similarly, the DOM property corresponding to the for attribute is htmlFor.


To use a reserved work as a key in an object literal, just wrap it in quotes, like this:

new Element(
 'label', {
  'for': 'radioButtonId'
 }
).update('label text');


Have you tried putting for in quotes?

new Element('label',{'for':'radioButtonId'}).update('label text');
0

精彩评论

暂无评论...
验证码 换一张
取 消