Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery How to set attribute of element using other attribute of the same element

Tags:

jquery

I want to set title text of a link calling a function which receives as parameter the id of the element and outputs the text.

something like

$(a).attr('title', function() {return $(this).id + "foo"});

but a construct like this doesn't exist as far I know. What can I do? Thanks.

like image 310
User Avatar asked Mar 13 '12 14:03

User


1 Answers

Use $(this).attr('id') or this.id. Do not mix them.

$(a).attr('title', function() {return $(this).attr('id') + "foo"});
$(a).attr('title', function() {return this.id + "foo"});        // <-- Preferred
//^ Is this a a variable? If not, you have to quote it: $("a").attr( ... );
like image 113
Rob W Avatar answered Sep 26 '22 18:09

Rob W