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

Thanks
Neil


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.9/css/all.css" >
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<style>.red { color:#F00;}</style>
</head>
<body>
<a href="#" id="favourite">
    <div class="profileHeaderActionButton _icoFavourite">
        <i class="fas fa-heart"></i>
        <span class="hideMobile">Favourite</span>
    </div>
</a>
<script>
	$(document).ready(function(){
		$("#favourite").click(function(){
			$.ajax({url: "/utilities/favourite/", async: false, success: function(result){
				alert(result);
				$( "div ._icoFavourite i" ).addClass(result);
			}});
		});
	});	
</script>                
</body>
</html>

Open in new window

LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?

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

x
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.
Why?
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.
Why?
Because
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
$(function(){
  $("#favourite").click(function(e){
    e.preventDefault();
    $.ajax({
      url: "/utilities/favourite/"
    }).then(function(result){
      $( "div._icoFavourite i" ).addClass(result);
    });
  });
});  

Open in new window

Working example here
0

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.
Neil
0
Julian HansenCommented:
You are welcome.
0
Neil ThompsonSenior Systems DeveloperAuthor Commented:
another related one just going up now
0
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
jQuery

From novice to tech pro — start learning today.