0

I have always disliked using ids and classes as selectors for obvious reasons: ids are unique and classes have to do with styles and should be able to change without affecting functionality of the site.

I used to create things like

   <div rel="foo">...</div>

   $("[rel=foo]").click(..);

What is the proper way to do this with HTML5? I was tempted by role, but it appears, this attribute has a specific meaning and purpose.

I am just learning HTML5 and your help is greatly appreciated!

1
  • 1
    I think your supposition that "classes have to do with styles" is faulty. From the HTML 5 spec: "..authors are encouraged to use values that describe the nature of the content, rather than values that describe the desired presentation of the content." Classes change the semantics of an element, they do not -- inherently -- describe how it looks. An element's visual style is a by-product of its meaning (only one of which are its classes), not the other way around. Commented Jun 9, 2012 at 15:16

3 Answers 3

1

You can add any number of classes to your element. And when you selecting, you can use one class as selector.

<div class="class1 class2 foo">

selector,

$(".foo").click(..);

you don't need to defined foo class inside your CSS

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

1 Comment

I do like your point. I dislike using classes as so specific to the element that they are not useful for others, e.g. class="button-on-signup-form" which I see in so many sites. I prefer the class="col notepad private" or something, indicating a variety of things I have defined to mean "this kind of a thing." Adding "collapsible" to it and tying it to functionality doesn't feel improper. I'll adopt the idea.
0

Try not using rel, choose title instead. Then you can use jQuery like this:

$('div[title="foo"]').click(...);

Using title, id or class is proper for HTML5. Also, I think you should revisit using classes and IDs as selectors - it's very handy, efficient and easy.

1 Comment

While I do not like using title (it has a specific usefulness), I looked up rel and you are right - it's worse: "The REL attribute is used to give the relationship(s) described by the hypertext link, and describes the relationship of the destination of the link to the document containing the hypertext anchor -- consequently REL cannot be used unless an HREF is present. The value for REL is a space-separated list of relationship values." I'll stick with using classes as described by all the answers here. Thanks!
0

Don't go against the wildly accepted solution here: use the class attribute.

Try to elaborate a naming convention that maps the structure of your pages and components in these pages.

Comments

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.