I've generally tried to stick with DOM-only attributes when writing Javascript. Now that I've switched from Prototype to jQuery, I can get some serious mileage out of adding my own attributes to various DOM elements, mostly in the realm of being able to set up a very readable coding convention for handling AJAX requests.
As a short example, this means I do things like
<div type="book" app_id="13">
<a href="#" action="delete">delete</a>
</div>
And then I can set up code to find all <a>
tags with an action
attribute, find a parent with a type
and app_id
, and then do CRUD operations... all without me having to write additional code.
Are there any pitfalls (other than not being strictly XHTML complaint) that I should watch out for, and/or any good habits I should look to emulate? How about a standard way of setting up my own attribute namespace?
Non-standard attributes can be fairly useful for passing along meta-data to Javascript however. For instance, if a link is suppose to show a popup, you can set the name of the popup in an attribute: <a href="#null" class="popup" title="See the Popup!"
HTML is the foundation of a webpage.
An HTML attribute is a piece of markup language used to adjust the behavior or display of an HTML element. For example, attributes can be used to change the color, size, or functionality of HTML elements. Attributes are used by including them in an opening HTML tag: <tag_name attribute_name="value">Content</tag_name>
Attributes are additional information which we can put in the HTML to initialize certain DOM properties. Properties are formed when the browser parses the HTML and generates the DOM. Each of the elements in the DOM have their own set of properties which are all set by the browser.
According to this question, using XML namespaces in XHTML 1.0 is invalid. Adding your own attributes to the same namespace seems worse to me, as they're most certainly invalid, even as far as XML goes.
Were I doing this, I'd get my mileage out of class
and rel
attributes. For example:
<div class="book" id="book_13">
<a href="http://example.com/url/to/delete/non/ajaxily" class="delete">delete</a>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With