11

I've been adding custom attributes which I reference using JQuery, this works great. But is it good practice?

example:

<div class="monkeys" customattr="big Monkey"> </div>

thanks all

2

2 Answers 2

18

Best practice is to use HTML5 data-* attributes:

<div class="monkeys" data-customattr="big Monkey"> </div>

This is standards-compliant HTML5, unlike arbitrary custom attributes. It also makes sure that your custom attribute won't conflict with some future standard attribute.

In recent (1.5+) versions of jQuery, you can also use $('.monkeys').data('customatrr') to access the attribute.

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

Comments

0

Yes, it is. If you're not using HTML 5 (see @SLaks post) why not change your class to be descriptive?

<div class="big monkeys"> </div>

3 Comments

Now you're assigning two classes: big and monkeys. Point taken, though, to use "bigMonkeys" or similar.
Yeah, but he probably already has styling for monkeys. And what if there are "small monkeys" or "baby monkeys". Taking a more OO approach is better unless he doesn't have that situation. However, I'm guessing this question wouldn't come up if that were the case.
Fair enough. If that fits the situation, I'm all for it.

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.