Brandon Brandon - 3 months ago 6x
jQuery Question

Append text to an attribute rather than replacing it?

I want to append some text to the title attribute of all elements of a certain class. This works to replace the text:

$('.theclass').attr("title", "Replaced text.");

It seemed logical that this would do the trick:

$('.theclass').attr("title", $(this).attr("title") + "Appended text.");

But instead I end up with an empty title attribute. Any ideas?


In that context, $(this) is not the element of $('.theclass'). Maybe you want to use each:

$('.theclass').each(function() {
    $(this).attr("title", $(this).attr("title") + "Appended text.");