2

Usually when I have a div, I use the id attribute to uniquely identify an element.

Ex:

<div id="F123" class="sidebar-item">
    My first item goes here
</div>

Now, I need a double 'key' for my div element: a 'slug' attribute and a 'category' attribute.

E.g.:

<div slug="my-first-item" category="techno" class="sidebar-item">
    My first item goes here
</div>

The compilator warns me that these attributes are not valid. What is the best way to have a double key on a div? I'm pretty sure there are various ways to proceed but I'm curious about the best way?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Bronzato
  • 9,438
  • 29
  • 120
  • 212

4 Answers4

6
<div data-slug="my-first-item" data-category="techno" class="sidebar-item">
    My first item goes here
</div>

New HTML data attributes. Why do think you need it if I may ask?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
2

HTML5 allows you to user data-[info].

<div data-slug="my-first-item"></div>

https://developer.mozilla.org/en/HTML/Global_attributes

Trevor
  • 11,269
  • 2
  • 33
  • 40
0

One way to do this would be to use a class and an id. An element can have any number of classes, so you can create a class for each category (with no styles), and then use the id to be more specific (although each id will still have to be unique, even across classes).

Another alternative is to have each id be of the form id="<category>_<slug>", and find ids that start with the category to get all the elements in a category.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
0

I don't know what or how you are going to process the date but this is my idea:

<div id="name['slug_value']['category_value']" class="sidebar-item">
    My first item goes here
</div>

Which translates to:

<div id="name['my-first-item']['techno']" class="sidebar-item">
    My first item goes here
</div>

But like the first answer I'm curious about why do you need to do this.

Kusanagi2k
  • 132
  • 5
  • 13