Add some hues to the icons

I am a beginner when it comes to CSS and jQuery. My goal is to color in an icon, similar to Instagram's heart icon which turns red when double-tapped.

Currently, I am using Font Awesome for this purpose. However, I am struggling to fill the color as desired. Changing the background-color property has not yielded the results I want.

Essentially, I would like the heart icon to change to a red color when a user clicks on it. Can anyone provide guidance on how to achieve this?

My HTML file
<!DOCTYPE html>
<html>
<head>
  <title></title>
<script type="text/javascript" src="all.js"></script>
</head>

<body>
<div id="sp"><i class="far fa-heart" id="heart" style="color: green; background-color: red" ></i> </div>
</body>
</html>
The "all.js" file contains the icons that I downloaded.

If Font Awesome doesn't work out, are there any alternatives you can recommend?

Answer №1

Why bother trying to create something already available? You can easily style icons without reinventing the wheel.

Instead of creating your own styles, consider using FontAwesome. FontAwesome provides different classes for each icon, such as using fa-heart for a filled heart and fa-heart-o for an empty one.

All you need is a simple jQuery code that toggles between these two classes when the icon is clicked:

$("#heart").click(function() {
  $(this).toggleClass('fa-heart-o');
  $(this).toggleClass('fa-heart');
});

Check out this demo:

$("#heart").click(function() {
  $(this).toggleClass('fa-heart-o');
  $(this).toggleClass('fa-heart');
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<i class="fa fa-heart" id="heart"></i>

Answer №2

To enhance the appearance of an element, you can apply a CSS class to it. In this scenario, I incorporated a click listener to the element and simply toggled the CSS class.

Here is the JavaScript code snippet:

(function() {
  const heart = document.getElementById('heart');
  heart.addEventListener('click', function() {
    heart.classList.toggle('red');
  });
})();

Check out the example here:

https://codepen.io/bojandevic/pen/mxKZqK

Answer №3

To make the icon change color when clicked, you must add an onclick function to it and create a corresponding JavaScript function.

<i class="far fa-heart" onclick="changeColor()" id="heart" style="color: green; background-color: red" ></i>

Here is how your JavaScript code should look:

function changeColor()
{
   var icon = document.getElementById('heart');
   icon.style.color = "red";    
}

Answer №4

By utilizing jQuery's toggleClass function, you have the ability to seamlessly include or exclude classes from each element within the specified elements.

$("#heart").on("click", function() {
  $(this).toggleClass('oldColor', 'newColor');
});
.newColor {
  color: red;
}

.oldColor {
  color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <script type="text/javascript" src="all.js"></script>
</head>

<body>
  <div id="sp"><i class="far fa-heart oldColor" id="heart">Heart</i> </div>
</body>

</html>

Answer №5

If you want to change the color of an element on click, you can achieve this by using the onclick attribute in HTML like so:

<div id="sp">
    <i class="far fa-heart" id="heart" onclick="changeColor()" style="color: green;" ></i>
</div>

Then, in your JavaScript code area or file, define a function named changeColor that changes the color of the heart icon to red:

function changeColor() {
    document.getElementById("heart").style.color = "red";  
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Angular Syncfusion schedule is unable to call upon an object that may potentially be 'undefined'

Currently, I am developing an application using Angular Syncfusion to allow users to view and book appointments. I found a helpful resource at the following link: Below you can find the code snippet I have been working on: <ejs-schedule #scheduleObj ...

Step-by-step guide on showcasing a quiz utilizing Magnific-popup and ajax

My goal is to create a functionality that allows users to easily download and view a quiz in a lightbox with just one click. I have successfully set up the ajax features and believe I am using Magnific-popup correctly, but for some reason, the lightbox is ...

"Combining Laravel's Joining Table Feature with the Power of JQuery and AJAX Calls

I am in the process of combining several tables and retrieving data to display in datatables. Currently, I am utilizing yajra datatables. Below are the tables that I wish to join: ekspedisis : id, client_id, org_id, expedisi_id, expedisi_name, status, c ...

Tips for beginner on converting form data to JSON using jQuery

I'm currently working on using AJAX to send data to a PHP page. I have all the form data and I want to convert it to JSON. However, when I use .stringify(), it doesn't seem to work as expected. Below is the code snippet: <script> ...

Ways to disable .animate() specifically for a particular element

I have implemented a countdown using which smoothly animates the numbers down and fades to opacity 0. Update. I have also created a demo on jsFiddle here: http://jsfiddle.net/Mw4j2/ // The .static class is added when the animation // complet ...

Dynamic Population of Django Drop Down List using JavaScript

In my Django + Python website, users can request access to a database through a form that provides them with the following options: Environment: A dropdown list with two values - Development and Production. Permission: Another dropdown list with two val ...

Using jQuery to trigger alert only once variable has been updated

I have a question that may seem too basic, but I can't find the solution. How do I make sure that the variables are updated before triggering the alert? I've heard about using callbacks, but in this case, there are two functions and I'm not ...

Align text within HTML with the use of Bootstrap

Example data value category type item1 100 food fruit item2 50 drinks soda item3 75 snacks chips I want to achieve this forma ...

The CPU is maxed out at 100% utilization

I need to continuously fetch XML data from another server every 15 seconds, then convert it into JSON and store it in a new file on my server. Below is the jQuery code I am using: (function($){ function getData() { $.ajax({ url: & ...

What steps should I take to design a mobile-friendly navigation bar?

I have shared the code for my navigation bar in a previous post Fixing Nav Bar to top of page. Now, I want to create a mobile version of it, but I am new to designing for mobile devices. Based on feedback and some CSS tweaking, I have managed to make it wo ...

Store data in Laravel's storage directory

I encountered an issue while trying to save files to the storage folder in Laravel after deploying the project on a web server (byethost7.com). The files were only being stored in the public folder. I ran the following command in the command prompt: >p ...

Error: TypeScript is unable to find the property URL within the specified type. Can you help

Currently, I am honing my skills in TypeScript and encountering a problem. I am a bit confused about how to define the type for the URL when I am setting the mix here. Obviously, the URL is supposed to be a string, and this specific scenario is within the ...

What is the best way to showcase a specific column from a dataset using Vue.js and axios?

Hey there, I'm still getting the hang of all this and haven't quite mastered the jargon yet. Here's what I need help with: I managed to retrieve data from my SQL database using axios, and when I check in (f12) - network - preview, it shows: ...

What is the best way to access the original observed node using MutationObserver when the subtree option is set to

Is there a way to access the original target node when using MutationObserver with options set to childList: true and subtree: true? According to the documentation on MDN, the target node changes to the mutated node during callbacks, but I want to always ...

View 360-degree panoramas on an iPad using the krpano HTML

I am currently experimenting with the KRPano HTML5 panorama viewer for a specific page on tablets/iPads. The flash viewer works seamlessly on desktops: embedpano({ swf: "/Files/Flash/krpano.swf", xml: "/Files/Flash/view.xml", target: "panview", id: "krpan ...

What can I do to prevent cookies from being deleted when I refresh the page or change the window location?

Struggling to maintain session in the frontend, I discovered that my cookie keeps disappearing whenever I refresh the browser or change the window.location. angular .module('myservices') .factory('myAuthentication&apos ...

What is the best way to incorporate a div below an image in Ubergallery?

I have integrated the jquery ubergallery into my website, and I am trying to add a new div below the main photos for additional content such as share buttons and comments. I tried using the code that worked with colorbox, but since ubergallery uses colorbo ...

Send the JSON file to the server by uploading it

Situation Currently, I'm dealing with a page comprising questions structured as shown below: sections:[{ section: 1, questions:[{ question: 1, attachment: [FormData Object] ... }, { question: 2, ...

Angular 7 routing glitches causing page refresh issues

Here is the issue I'm facing: I am looking for a way to switch between tabs in my navigation bar without having to refresh the entire website. I have just started working with Angular and I believe that the router should be able to route to a new pag ...

Creating a visual list in a React JS component with images and text

Hey there, I'm new to react js and I'm looking to create a list with images and text using html, css, and bootstrap. The list should include song images and have options for download and liking them after the user clicks on three dots. I've ...