Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes on custom HTML elements

When working with custom HTML elements in web components, should I still name custom attributes with the prefix data?

For example:

<!-- Should form be data-form? -->
<my-button form="foo">click me</my-button>
like image 472
Ben Aston Avatar asked Feb 24 '15 22:02

Ben Aston


People also ask

Can HTML elements have custom attributes?

Every HTML element may have any number of custom data attributes specified, with any value.

What are attributes of HTML elements?

HTML attributes are a modifier of an HTML element type. An attribute either modifies the default functionality of an element type or provides functionality to certain element types unable to function correctly without them. In HTML syntax, an attribute is added to an HTML start tag.

How do you set data attributes in HTML elements?

Creating an HTML Data Attribute Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.


2 Answers

No, that's not necessary.

Existing HTML elements have a defined set of attributes, which means you would invalidate the HTML by just adding any attribute. By introducing the data- attributes, it was made possible to extend existing elements without invalidating them.

Web components are custom elements. They have no defined set of attributes, you define them yourself. Whether or not you use data- attributes is completely up to you, but you don't have to. Your component cannot become invalid because there is no definition of valid for it.

If you care about semantic/valid HTML, this answer may be relevant to you too: Are custom elements valid HTML5?. In short: use a dash in your component name to make sure it's picked up as valid HTML.

like image 164
Stephan Muller Avatar answered Sep 20 '22 16:09

Stephan Muller


In general, there is no difference between custom elements and predefined. You might create an element of your choice with document.createElement and register it with document.registerElement. The result would be:

console.log(document.createElement('my-button').constructor);
//⇒ function HTMLElement() { [native code] }
console.log(document.registerElement('my-button'));
//⇒ function my-button() { [native code] }
console.log(document.createElement('my-button').constructor);
//⇒ function my-button() { [native code] }

As you could see, once registered, the element is awarded with it’s own constructor. That provides an ability for components to behave in it’s own way. But:

console.log(document.createElement('my-button').__proto__.__proto__);
//⇒ HTMLElement

is still a good old plain HTMLElement. So, the rules for attribute naming were not changed.

Please note, that libraries, like polymer, might add additional attribute handling; the above is true for plain web-components only.

like image 40
Aleksei Matiushkin Avatar answered Sep 20 '22 16:09

Aleksei Matiushkin