1

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

3 Answers 3

5

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

Class Selector (“.class”)

Sign up to request clarification or add additional context in comments.

2 Comments

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.
The class attribute takes a space separated list of classes. Give the elements multiple classes. class="transport bus".
1

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.

1 Comment

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.
-3

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.