Juan Sebastian Totero
Juan Sebastian Totero

Reputation: 480

Custom values into HTML elements as attributes?

I'm currently working on a very simple calendar using Jquery.

Actually, We need a Weekly view for some events, with the possibility to change the week using Jquery (like on Google Calendar).

The easiest way I thought is wrapping each week into a <div>, and with Jquery moving along the different divs / weeks in a slider-like style.

Actually I need a way to number the single divs, and I was thinking to use a "virtual" attribute week-number, i.e.

<div week-number="3"> ... </div>

That's ok when moving to next weeks, but to go to the previous one? Logically, I would get negative week numbers, providing the starting week is numbered "1", that's to say

<div week-number="-3"> ... </div>

I really don't like so much this approach, so have you any idea about identifying the different divs / weeks?

EDIT : Thinking about it: what about using numbered classes? as example class="week-1",class="week-2", etc. ?

Upvotes: 2

Views: 2614

Answers (2)

andreszs
andreszs

Reputation: 2956

Try adding the value attribute, and then you can read it with the standard JavaScript getAttribute('value') method.

<div value="3"> ... </div>
<div value="-3"> ... </div>

Or even easier, use the id attribute and put the number there.

This attribute will be silently ignored by any browser and no warnings will be raised. I always avoid implementing HTML5 for features that can be implemented in HTML4 somehow.

Upvotes: 0

Erick Petrucelli
Erick Petrucelli

Reputation: 14932

This is why data attributes exists: http://ejohn.org/blog/html-5-data-attributes.

And of course you can be with a valid XHTML: How can I use HTML5 Data Attributes in XHTML?.

About your edit, I emphasize even more the use of data attributes. What you suggested works, but is merely a workaround to fix something that already has a right solution.

Even you could include in your project Modernizr if you really believe may have problems with older browsers. Well, I never had these problems.

Upvotes: 7

Related Questions