views:

62

answers:

3

Hi,

What is your opinion about using non valid attributes on html elements for easier jQuery selectors etc ?

Eg.

<div name="myDiv"></div>

According to Visual Studio the name attribute is not valid for a div element.

+1  A: 

If you absolutely must, use the data pseudo-namespace; such as data-id or data-name.

Edit: Using the data pseudo-namespace is far better than perverting the usage of an existing attribute like class or id.

Williham Totland
This is a fine approach for adding metadata, but you would need something like John Resig's jQuery Metadata plugin to make it compelling for use with jQuery.
James H
A: 

If you still wanted to remain w3c compliant, the w3c allows title attribute for extra information, which you can use in lieu of adding new non-standard attributes.

Nitrodist
The title attribute is for advisory information about an element, it is **presented** to users. It is a **very** poor choice of attribute to use to store arbitrary data that you want to access via JavaScript.
David Dorward
+5  A: 

I would recommend using "dummy" classes (and the jQuery class selector) to disambiguate naming of html elements. 100% compliant approach.

Class Selector (“.class”)

James H
The problem for me when using the class is that it's a conflict with my css. I want to have the same css for a bunch of elements but want to query only a handful of them at a time so I need another discriminator attribute.
Marcus
The class attribute takes a space separated list of classes. Give the elements multiple classes. `class="transport bus"`.
David Dorward
@david: thank you, didn't think of that one
Marcus