What are the advantages of using data- rather than x- prefix for custom attributes?

All we need is an easy explanation of the problem, so here it is.

AngularJS documentation says Optionally the directive can be prefixed with x-, or data- to make it HTML validator compliant..

Example markup:

  1. no prefix: <input ng-model="name">
  2. data- : <input data-ng-model="name">
  3. x- : <input x-ng-model="name">

The x- prefix is faster to type than data-, but the tutorials I have seen used either no prefix or data-, so my question is: are there any reasons I might want to use data- rather than x-?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The advantage of using a data- prefix rather than an x- prefix is that data- prefixes are guaranteed to remain available to authors as they like, whereas an x- prefix might one day be used for a browser-specific purpose, possibly conflicting with yours. Note that x- are not conforming according to HTML5.

Method 2

data- is a standardized part of the HTML5 spec. Semantically it’s the best way to go.

Method 3

To avoid conflicts between your application attributes and future attributes that could be defined in HTML, the w3.org defined and normalized a specific scheme, the data-* attribute. See the norm.

Use it

  • to reduce the risk of incompatibilities
  • to let other coders and tools recognize that those attributes contain application specific data

Besides, using the normalized data attribute lets you use the element.dataset property and some libraries, for example jQuery, provide you specific utilities.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply