Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to an attribute rather than replacing it?

Tags:

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

like image 420
gravitation Avatar asked Mar 13 '09 22:03

gravitation


People also ask

How to append attribute value in jQuery?

Syntax: $("#add-attr"). click(function () { $("#addAttr"). attr("placeholder", "GeeksforGeeks"); });

What is the purpose of the method append()?

append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .

What is the opposite function of append?

Show activity on this post. The opposite of . append() is . prepend() .


2 Answers

I know this question is 7 years old. Just in case someone came across this question, here's the solution without using .each:

$('.theclass').attr("title", function() { return $(this).attr("title") + "Appended text." }); 
like image 129
Kharisma Wijaya Avatar answered Oct 20 '22 07:10

Kharisma Wijaya


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."); }); 
like image 22
strager Avatar answered Oct 20 '22 06:10

strager