Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do html5 data attributes need a value? [duplicate]

I am wondering if html data attributes actually need a value to be applied?

I wonder this because often all we want to know is if the attribute is actually set to act as a flag. (sure we could use a class for this; but realistically unless you are going to style these items differently then the flags are more data than a semantic item).

A perfect example of this is if we want a link to scroll to it's target instead of jumping our jQuery code might look like:

$(document).on('click', '[data-scroll-link'], function(){/**do scroll**/});

I know in google chrome it is sufficient for the anchor to appear as

<a href="#bottom" data-scroll-link>Scroll to bottom</a>

But will that work everywhere? and is it even valid HTML5 (I believe it is due to the autofocus, autoplay etc attributes). or do we need:

<a href="#bottom" data-scroll-link="true">Scroll to bottom</a>
like image 604
Hailwood Avatar asked Jul 26 '13 03:07

Hailwood


People also ask

Do HTML attributes need values?

We Suggest: Always Quote Attribute Values The HTML standard does not require quotes around attribute values. However, W3C recommends quotes in HTML, and demands quotes for stricter document types like XHTML.

Should attributes always have a value?

Note: Attribute values are generally case-insensitive, except certain attribute values, like the id and class attributes. However, World Wide Web Consortium (W3C) recommends lowercase for attributes values in their specification.

What is data attribute in HTML5?

The data-* attribute is used to store custom data private to the page or application. The data-* attribute gives us the ability to embed custom data attributes on all HTML elements.

Can an attribute be applied multiple times to the same element?

Using the same attribute name twice in a tag is considered an internal parse error. It would cause your document to fail validation, if that's something you're worried about. However, it's important to understand that HTML 5 defines an expected result even for cases where you have a "parse error".


2 Answers

No. But...

As is common with all attributes, in the application/xhtml+xml serialisation, XML rules apply and the attribute must have an explicit name and (quoted) value.

So this question is really about the text/html serialisation, and therefore the relevant part of the HTML5 spec is Section 8 The HTML syntax

In particular, under attributes, it says:

Attributes can be specified in four different ways:

where the first of these is:

Empty attribute syntax

   Just the attribute name. The value is implicitly the empty string.

It's necessary to understand though that the value is of string type, not of boolean type.

For example, with <input id="cb" type="checkbox" checked>, the "checked" attribute is reflected by a property that is either true or false. So

if (document.getElementById("cb").checked)

will evaluate to true for the above markup.

In contrast, with <input id="cb" type="checkbox" data-checked>, the "data-checked" attribute is reflected via the dataset object as a string. The value of this property is the empty string, which in JavaScript is falsey. So,

if (document.getElementById("cb").dataset.checked)

will evaluate to false for the above markup.

To do the equivalent test, compare the value for "not undefined". I.e.

if (document.getElementById("cb").dataset.checked !== undefined)

will evaluate to true for the above markup.

See http://jsfiddle.net/GAxvW/

like image 186
Alohci Avatar answered Oct 24 '22 16:10

Alohci


Simple Boolean Test For Element Attributes

To expand on Alohci's excellent answer, the following is a simple, flexible way to test for a true boolean attribute value supplied using one of three standard HTML conventions: <foo data-bar/>, <foo data-bar="true"/>, or <foo data-bar="data-bar"/>.

var a = elem['data-bar'];
var aTrue = ( a != null && a !== false && a !== 0 && a.charAt(0) != 'f' &&
              a.charAt(0) != 'n' );

With the code above, the value is false if undefined or set to one of: f*, n*, 0 (case-insensitive), and true if defined and set to one of: (empty string), (attribute name), (anything else).

Empty strings are evaluated to true here because HTML attributes without values are '' which equal false in JS (and something like <foo disabled/> should equal <foo disabled="true"/>). You can use the above code for more general string testing by removing != null && a !== false.

like image 20
Beejor Avatar answered Oct 24 '22 15:10

Beejor