add axax return from jQuery call to existing class in <i> tag

Hi all

Not great on jQuery and after something quite simple.

Upon clicking the anchor contents (<a href="#" id="favourite">) below I want to add a class to the fontAwesome icon which is what is coming back from the AJAX call to ($.ajax({url: "/utilities/favourite/", async: false, success: function(result){

For this basic example it returns either "green" or "red", simple as that

How can I get it to add this returned text to <i class="fas fa-heart"></i> please


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="" >
<script src=""></script>
<style>.red { color:#F00;}</style>
<a href="#" id="favourite">
    <div class="profileHeaderActionButton _icoFavourite">
        <i class="fas fa-heart"></i>
        <span class="hideMobile">Favourite</span>
			$.ajax({url: "/utilities/favourite/", async: false, success: function(result){
				$( "div ._icoFavourite i" ).addClass(result);

Open in new window

Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
First up don't use async: false - that is a very bad practice.
AJAX by nature is meant to be Asynchronous - you need to code your application to be async aware rather than forcing the AJAX call to be synchronous.

Next your code is almost correct just remove the space between div and ._icoFavourite.
div ._icoFavourite => means find the child elements of all div's where the child has the class _icoFavourite
div._icoFavourite  => means find the div with the class _icoFavourite

Additionally use the .then() on the returned Promise to handle the callback rather than success.

Finally, you can use $(function() as a shortcut instead of $(document).ready(function()
They are functionally the same thing and the former requires less typing.

Here is the updated code
      url: "/utilities/favourite/"
      $( "div._icoFavourite i" ).addClass(result);

Open in new window

Working example here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Thanks for the code and great explanation Julian, greatly appreciated.
Julian HansenCommented:
You are welcome.
Neil ThompsonSenior Systems DeveloperAuthor Commented:
another related one just going up now
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.