Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery : Is it bad practice to use a custom attribute in my html code?

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

like image 311
Funky Avatar asked Oct 06 '11 13:10

Funky


1 Answers

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.

like image 136
SLaks Avatar answered Oct 23 '22 16:10

SLaks