Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple values in one HTML "data-" element?

Tags:

html

css

Can I have multiple values in one HTML "data-" element? Similar to how a class can have multiple class names.

If possible, I would like to create a CSS/JS library that makes use of one "data-" element to house all of the library styles. For example:

<div data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>

That way, any of the programmers custom style rules can go into the elements class. My reasoning for this is to make readability easier, so together it would look like:

<div class="custom-style another-style" data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>
like image 755
Mark Beleski Avatar asked Dec 24 '15 15:12

Mark Beleski


People also ask

Can an HTML element have multiple data attributes?

You can have multiple data attributes on an element and be used on any HTML element.

How do you make multiple values in HTML?

Definition and Usage For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

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".

How do you add two style attributes in HTML?

You can add multiple styling properties at once when using the HTML style attribute - just make sure to separate the name-value pairs with commas. Using a separate stylesheet is very convenient for styling multiple pages, as it's easier to apply changes to one document than to each page separately.


3 Answers

Can I have multiple values in one HTML "data-" element?

You can have a string. The spec doesn't define any particular format for the data in the attribute, which is designed to be processed by site specific JavaScript.

Similar to how a class can have multiple class names.

The class attribute takes a space separated list of classes.

Your JavaScript can your_data_attribute_value.split(" "); if you like.

Handling this with CSS would use the ~= attribute selector.

[att~=val]
Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

like image 184
Quentin Avatar answered Sep 30 '22 03:09

Quentin


CSS has the shortcut .class selector but it actually is parsing the attribute named "class" as a list for space separated values. This is supported in the non-shortcut form by the following attribute selector:

[att~=val]

Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly "val". If "val" contains white space, it will never represent anything (since the words are separated by spaces). If "val" is the empty string, it will never represent anything either.

Ref: http://www.w3.org/TR/CSS2/selector.html#class-html

As your question is tagged CSS you're perhaps looking for that. The rules how the parsing of attribute values is done is given in that document as well, so in case the javascript library you're trying to use on this (if any) won't cover that, it should be easy to add:

var list = $("div").data("library-name").split(/\s+/);
                                         ^^^^^^^^^^^^

This split with the white-space regular expression parses the string attribute value into an array with javascript and the Jquery library (for accessing the DOM and the data attribute).

like image 25
hakre Avatar answered Sep 30 '22 03:09

hakre


AFAIK, I don't think data- attributes can convert that to an array. Instead, I think it'll interpret it as one value, but it is allowed.

If you want to do that, you'll probably have to split() it later in JavaScript into an array of usable values.

See this example on JSFiddle.net.

like image 23
Jonathan Lam Avatar answered Sep 30 '22 04:09

Jonathan Lam