Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misc information HTML element attributes

Tags:

html

I've been using rel to store some non-html information, what other attributes are not commonly used that I can utilize to store information?

like image 981
Josh K Avatar asked Nov 22 '10 03:11

Josh K


People also ask

What are HTML elements and attributes?

HTML element holds the content. HTML attributes are used to describe the characteristic of an HTML element in detail. HTML tag starts with < and ends with > Whatever written within a HTML tag are HTML elements. HTML attributes are found only in the starting tag.

What are the 3 types of attribute in HTML?

HTML attributes are generally classified as required attributes, optional attributes, standard attributes, and event attributes: Usually the required and optional attributes modify specific HTML elements.


7 Answers

You might want to switch to HTML5 and use custom data attributes.

like image 84
Gert Grenander Avatar answered Oct 04 '22 17:10

Gert Grenander


You can make up your own attributes if you wish. It is probably not a good idea to put data into an attribute if by specification it should have something meaningful in there.

like image 29
Marcus Whybrow Avatar answered Oct 04 '22 15:10

Marcus Whybrow


In the HTML 5 spec, you can use data-* attributes; they're guaranteed not to do anything with the browser and they'll work in older browsers, too.

In JavaScript, you can access it with the normal attribute properties:

var value = elem.getAttribute("data-foo")
elem.setAttribute("data-foo", "value")
elem.removeAttribute("data-foo")

With new browsers you can use elem.dataset, but you probably don't want to do that as older browsers won't support it.

var value = elem.dataset.foo;
elem.dataset.foo = "value";
elem.dataset.foo = null;
like image 25
Chris Morgan Avatar answered Oct 04 '22 15:10

Chris Morgan


In HTML5 you can define your own data- attributes to store whatever you want.

like image 29
dteoh Avatar answered Oct 04 '22 17:10

dteoh


This help? Scroll down to the rel section:

http://diveintohtml5.ep.io/semantics.html

like image 20
Gregg B Avatar answered Oct 04 '22 17:10

Gregg B


HTML5 now supports storing information in data-* attributes but thats HTML5 and everyone isn't there yet. So a real world scenario...

If you don't have to worry about persisting across post backs you could map simple data objects as a JSON collection var and pull an object out of it based on a particular key. But that is a broad approach - more information on the overall goal would improve the feedback.

like image 38
meinmkv Avatar answered Oct 04 '22 17:10

meinmkv


Depends on what kind of information you're looking to store and how are you planning on accessing it. I successfully used both the id and the class attributes to store and access database IDs for example, and even references for some database tables, which I then retrieved with jQuery to do some AJAX requests.

If you are NOT using HTML5, I would advise against using your own custom attributes. Otherwise, like people already suggested before me, have a look at the data- prefixed attributes.

like image 33
Valentin Flachsel Avatar answered Oct 04 '22 16:10

Valentin Flachsel