Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change colour of a heart from red -> green

I've tried some code, but it doesnt seem to change it when clicked.

Error i get is:
annot read property 'getElementsByClassName' of null

What I have tried:


		var header = document.getElementById("heart");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";

PHP ->
if (isset($arr['BName'][$i])) {
             echo '<td>' . $arr['BName'][$i] . '<button style="text-align:right;float:right;" class="favourites"  id="' . $arr['BName'][$i] . '" value="' . $dbRow["UserID"] . '<button class="btn" id="heart">❤️</div></button></td>';
         } else {
Updated 17-Apr-18 8:09am

1 solution

You need to tie an event to your button JavaScript Events[^]
Then you need to use DOM to change the color of the heart's font (i.e., changing style: JavaScript DOM CSS[^]

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900