Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

how to change data-icon value dynamically ?

I have this code of line in my HTML and i want to change it's attr value how i can insert value to do same...

<a href="#" data-icon="&#xea3c;"></a>
 $(this).attr("data-icon",&#xea3c; ); //here i want to insert value dynamically

1 Answer

Matt F.
Matt F.
9,518 Points

$('a').attr("data-icon", '' );

or

$('a').data("icon", '' );

I am not sure what you are looking to use this for, but you needed to target the anchor element (replace 'this' with 'a'), as well as wrap the second argument to attr/data a string.