hypno7oad
hypno7oad

Reputation: 1461

How do I use html labels with dijit/form widgets?

Ok, so I'm building an app using Dojo 1.8, and I have a custom widget with a template similar to this...

<div>
  <label for="tag">Select something: </label>
  <select id="tag"
      data-dojo-attach-point="tag"
      data-dojo-type="dijit/form/Select">
    <option value="0">option 0</option>
    <option value="1">option 1</option>
  </select>
</div>

However, when the template gets rendered, the widget defines a new id, which makes the tag useless. I've tried googling this, but all my searches just direct to the Dojo documentation since they have attributes called labels but have nothing to do with the HTML label tag.

What is the proper why to do this?

Upvotes: 0

Views: 2860

Answers (1)

Stephen Simpson
Stephen Simpson

Reputation: 1381

In the situation you describe, you can simply place the label around your <select> and dispose with the for/id attributes. see Stackoverflow question:

Also, if you want to actually use Ids in a widget template, see:

Using ids directly (ie. hard-coding them, not assigning them on-the-fly as in the above link) is not encouraged. The reason for this is that a template is meant to used over and over again in the creation of widgets.

In theory, it could be used to create multiple widgets on one page. Hence, in that situation you would have an id conflict. Each HTML id, on any one page, needs to be unique.

Upvotes: 1

Related Questions