JQuery How to set an attribute of an element using a different attribute of the same element

I want to set the title text of a link that calls a function that takes an element identifier as a parameter and displays the text.

sort of

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

but such a design, as I know, does not exist. What can I do? Thanks.

+4
source share
4 answers

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

 $(a).attr('title', function() {return $(this).attr('id') + "foo"}); $(a).attr('title', function() {return this.id + "foo"}); // <-- Preferred //^ Is this aa variable? If not, you have to quote it: $("a").attr( ... ); 
+7
source

Use jQuery .each() method:

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

Links: jQuery .each()

+1
source
 var id = $(this).attr('id') ; //capture the caller $(a).attr('title',id + 'foo'); 
+1
source
 $('a').each(function(){ $(this).attr('title', $(this).attr('id')); }); 

May be,?

0
source

Source: https://habr.com/ru/post/1401190/


All Articles