This is not quite to the point of discussing the merits per se, but
still germane I think. RobG pointed out that you're using the name
attribute in a div, which isn't valid. It's also not valid in a td,
but obviously that only matters if validation is important to you
(which is a totally separate discussion). Using classes is reasonable
depending upon the type of data you want to store, but for the example
you're giving, why wouldn't an ID attribute work for you?

You could also rethink your approach; there's really no reason to
store the kind of data you are in the element, just to figure out what
was clicked—that can be done on the fly (at least with the example
markup you give):

$('td.clickme').live('click', function() {
    console.log('filter'+$('td.clickme').index(this));
});

If you aren't going to add more rows, you could use this (saves having
to run the selector for each click--you also wouldn't need live):

var tds = $('td.clickme').live('click', function() {
        console.log('filter'+tds.index(this));
});

You can also use plain old event delegation and set the click on the
table:

$('table').click(function(e) {
        console.log('filter'+$(this).find('td.clickme').index($(e.target)));
});




On Apr 14, 10:56 am, seasoup <seas...@gmail.com> wrote:
> 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-cor...
>
> What I'm looking for instead is a discussion of using custom
> attributes in html.
>
> Thanks,
> Josh Powell

Reply via email to