0

Any idea can I use .data attribute in HTML select box?I red HTML5 documents but I didn't find any information that could help me.

Is it legal:

<span>Select depatament</span>
<span>
    <select id="department" onchange="EnableSelectBox(this)" data-spacing="10cm">
        <option selected disabled>-Select-</option>
    </select>
</span>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Michael
  • 13,950
  • 57
  • 145
  • 288
  • Yes, it is perfectly fine. But, `onchange="EnableSelectBox(this)"` isn't that pretty! [More Info](http://webdesign.tutsplus.com/tutorials/all-you-need-to-know-about-the-html5-data-attribute--webdesign-9642) – lshettyl May 11 '15 at 11:52
  • You can use data-attributes pretty much everywhere. You can also [use self-made custom attributes](http://stackoverflow.com/a/25813336/1654265) pretty much everywhere (but try choosing names that won't be introduced later by the W3C specs, or you'll need to fix your code). – Andrea Ligios May 11 '15 at 11:52
  • [Every HTML element may have any number of custom data attributes specified, with any value](http://www.w3.org/TR/html5/dom.html#custom-data-attribute) – Andy May 11 '15 at 11:55

3 Answers3

1

Using data attributes

Any attribute on any element whose attribute name starts with data- is a data attribute.

To answer the question: Yes. You can use data attributes on any element.

1

Yes it fine to use data in Html5 tag. I don't know why you want to use in such a way, but you can use it.

for example like this

$(document).ready(function() { alert($('#department').data('spacing')); });

jsfiddle link

Dinkar Thakur
  • 3,025
  • 5
  • 23
  • 35
1

It works fine. Test in JS using:

alert(document.getElementById("department").getAttribute("data-spacing"))
Zee
  • 8,420
  • 5
  • 36
  • 58