2

If let's say I require an <img> to store a certain information(arbitrary snippets of metadata for JS) about that element.

e.g. <img class="slider-img" rel="1" src="image.png" />

So i.e in a slider, I would be able to grab that img with rel="1"

What are some alternatives instead of creating/misusing attributes (xbrowser && backward compatible)?

Here are some I found that's currently being used:

  • HTML5, data-*
  • rel attribute
  • class/id attribute
  • custom attributes (Dojo)
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
hamahama
  • 393
  • 2
  • 5
  • 17
  • 1
    What kind of information? With title and alt, it's visible to the user but could be nice for description for example. – stracktracer Aug 23 '11 at 09:05
  • You mentioned Dojo, but there is also .data() for jquery. – Matt Aug 23 '11 at 09:06
  • possible duplicate of [XHTML validation, custom namespaces and attributes](http://stackoverflow.com/questions/581279/xhtml-validation-custom-namespaces-and-attributes) – Sam Becker Aug 23 '11 at 09:08
  • Sorry, you could understand it as information not meant to be relevant any of the current attributes. Like date-added for example. In html5 i would do it like data-date-added="12-01-11" – hamahama Aug 23 '11 at 09:11
  • @Sam152 Mine is referring specifically to . – hamahama Aug 23 '11 at 09:12
  • I'd go for data-* attribute. Mostly because it's the way forward in the future. – Exelian Aug 23 '11 at 09:24

2 Answers2

0

You can use "longdesc" attribute, it is a valid w3 code, though it is not supported by any major browsers, so you can use it to store your information.

Tumay
  • 488
  • 2
  • 6
  • 16
0

XML allows for whatever you want for your elements.

Check out: http://www.bennadel.com/blog/547-jQuery-Custom-Selectors-Holy-Cow-That-Is-So-Badass-.htm

<img class="slider-img" rel="1" src="image.png" myOwnAttribute="MineNotYours"/>

It might not validate against an HTml5 validator, but it is still valid XML / HTML5. Newer browsers and older browsers work with it, they will just ignore what they don't know.

Another SO post on it: Is it OK to add your own attributes to HTML elements?

Community
  • 1
  • 1
Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69