Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Definable Attributes on HTML Elements?

What I need to do is be able to store some piece of data about an element.

For example, lets say I have a list item <li>, and I want to store some data about it in the element, like "This is element 1 from XYZ".

The only way I know how to do this (which I don't want to do if I can avoid) is this:

<li id='item1'>
  Item 1
  <!--This is element 1 from XYZ-->
</li>

<script>
  // read it something like this
  var e = document.getElementById('item1').innerHTML; 
  // then parse out the comment, etc.
  // --
</script>

What I really want is something like this (which I am fairly certain is not possible):

// example
<li id='item1' userdata='This is element 1 from XYZ'>Item 1</li>

.. of course, I would like to be able to access it somehow via javasscript.

Alternatively, is there some other way to achieve this?

Thanks -

like image 365
OneNerd Avatar asked Jan 08 '10 01:01

OneNerd


People also ask

What is user defined attributes in HTML?

Definition and Usage The data-* attributes consist of two parts: The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-" The attribute value can be any string.

Can I add custom attributes to HTML elements?

If you want to define your own custom attributes in HTML, you can implement them through data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.

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.

What is an HTML attribute?

HTML attribute reference Elements in HTML have attributes; these are additional values that configure the elements or adjust their behavior in various ways to meet the criteria the users want.

Can I add custom attributes to my HTML elements?

You can add custom attributes to your elements at will. But that will make your document invalid. In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-.

What are <a> and < href> attributes?

HTML attributes provide additional information about HTML elements. The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:

How do you define an HTML element?

An HTML element is defined by a start tag, some content, and an end tag. The HTML element is everything from the start tag to the end tag: My first paragraph. Note: Some HTML elements have no content (like the <br> element).


1 Answers

You can access your userdata="" attribute from JavaScript. Just do:

var theData = document.getElementById('item1').getAttribute('userdata');

If you want to do it the HTML5 way, then you would use attributes named data-*, e.g.:

<li id='item1' data-foo='This is element 1 from XYZ'>Item 1</li>

that way it will still be valid (i.e., it'll make you feel better for not using an invalid attribute). New browsers will support accessing the data-* attributes like so:

var theData = document.getElementById('item1').data.foo;

but I don't think that is implemented widely enough to rely upon yet.

If you do want to store the data in a comment (although I'd advise going the attribute route instead) you could do something like:

var e = document.getElementById('item1');
var n = e.firstChild;
while (n && n.nodeType != Node.COMMENT_NODE) {
    n = n.nextSibling;
}
// now n.nodeValue will have the comment contents

(No guarantees about whether IE likes any of the above.)

like image 80
heycam Avatar answered Nov 14 '22 21:11

heycam