Richard Knop
Richard Knop

Reputation: 83745

Is there a way to do this without each?

I have many table cells which I want to show a tooltip once hovered. Each td element has a rel attribute with a request URI for ajax to fetch content into a tooltip. Right now I do it like this:

$('table td.tooltipped').each(function() {
    var uri = $(this).attr("rel");
    $(this).bt({
        ajaxPath: uri,
        ajaxError: "<strong>Error!</strong> Here's what we know: <em>%error</em>."
    });
});

It seems a little redundant to me but I can't figure out how to do it without the each loop. Any ideas?

Upvotes: 0

Views: 117

Answers (2)

aorcsik
aorcsik

Reputation: 15552

It is fine with each, since .bt() probably can't handle the referencing of each elements itself in a parameter list. You can leave the uri declaration if you want, and spare a tiny bit of memory:

$('table td.tooltipped').each(function() {
    $(this).bt({
        ajaxPath: $(this).attr("rel"),
        ajaxError: "<strong>Error!</strong> Here's what we know: <em>%error</em>."
    });
});

Upvotes: 1

user113716
user113716

Reputation: 322572

From the documentation:

Ajax dynamic path:

AjaxPath is being read from the 'href' value of the link. This is accomplished by defining the ajaxPath as an array. The first value of the array is the jQuery code to find the path value (in this case "$(this).attr('href')")...

So it would seem that you can do this:

$('table td.tooltipped').bt({
    ajaxPath: ['$(this).attr("rel")'],
    ajaxError: "<strong>Error!</strong> Here's what we know: <em>%error</em>."
});

I'll let you test it.

Upvotes: 2

Related Questions