I was wondering what jquery developers opinion of adding custom
attributes to html tags is, and what your basis is for these
opinions?  Why is it a good idea, or why is it a bad idea?  What I
mean is this:

<div href="" myType="foo">content</div>

where 'myType' isn't in any specifications.  I've run into developers
who think this is a good idea, and those who think this is a bad idea
and I'd like to get a better sense of both sides of the argument.
Personally, I use them all of the time.  They are a great way to
preserve information for use with .live() in jQuery, among other
things, and since I append a long string to the DOM instead of
creating lots of little DOM nodes, I generally cannot use .data() to
save information on the individual nodes.

Example of a usage:

<div name="foo" myType="bar">click</div>
...
$('div[name=foo]').live('click', function () {
   console.log($(this).attr('myType'));
});

Example of why I cannot use .data():

var htmlString = '<table>';
for (var a = 0; a < 100; a++) {
    htmlString += '<tr><td name="clickme" nodeId="filter'+ a
+'">click</td></tr>';
}
htmlString += '</table>';
$('div[name=foo]').append(htmlString);
$('td[name=clickme]').live('click', function() {
    console.log($(this).attr('nodeId'));
});

To debate the merits of using this kind of append, please go to:
http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly

What I'm looking for instead is a discussion of using custom
attributes in html.

Thanks,
Josh Powell

Reply via email to