0

I am working on a website using HTML5. I have a jQuery script that shows a custom tooltip on all elements that have the title attribute. I also have a script that makes an alert message appear when the user clicks a picture. The alert message will say what the title attribute equals. Unfortunately the tooltip script and the alert script interfere with each other.

My question is:

  1. Can I make up an attribute?
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 2
    data-* is the way to go – Johan Jun 09 '13 at 19:21
  • Could you share some code that goes along with the question? It's not clear how they conflict. – Ja͢ck Jun 09 '13 at 19:22
  • Inventing your own attributes is possible, but it's a very bad practice IMHO. – Simone Jun 09 '13 at 19:23
  • Yes you can. You can create just about any attribute you'd like, but it won't be valid HTML, for that you would need data attributes, where you again can create any attribute you'd like as long as it starts with `data-*` and it will be valid in HTML5. – adeneo Jun 09 '13 at 19:29

4 Answers4

4

Not exactly, but HTML 5 provides data-*.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

In html5 , use data-XX to produce extra attributes.

see : http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
1

You can make an aditional attribute, just by naming it.

<img src="abc.jpg" data-opens="abc" data-test="abc" id="image" />

And access it in jQuery by typing

$("#image").attr("data-opens")..

Like:

alert($("#image").attr("data-opens") + " " + $("#image").attr("data-test"));

Edited, thanks to GCyrillus.

  • 1
    The good way is to add the prefix data- So you can clearly access to attributes you made up. – G-Cyrillus Jun 09 '13 at 19:27
  • Thanks! Also helpful. I would vote you up too, but that requires 15 reputation... again. –  Jun 09 '13 at 19:30
0

Specifically answering you question: you can make up attributes.

But your HTML will no longer be valid and since you are adding them just to store information, you should use de data-* form to add you own data storage for an element, plus it will still be a valid HTML

Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97