Wednesday, October 7, 2009

jQuery - using "this" in a .each() function

The "this" keyword can be used in two ways inside a jQuery callback function. If you use "this" in the callback below without putting the $() around it, you will get a reference to the HTML element selected by the $(".currency") class selector.

However, if you simply wrap the "this" keyword with $(), it magically somehow becomes a jQuery object, and you can use all the regular jQuery functions on it like .text() and .val()

$(".currency").each(function() {
$(this).text(formatCurrency($(this).text()));
});

0 comments: