Brandon Brandon - 1 year ago 85
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?

Answer Source

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.");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download