How to add and remove with onclick class in javascript

How to add and remove class on element using onclick on them.

If I click on 1.st symbol for the first time its class will add and if I click on the symbol a second time its class will be removed

If I click on 2.st symbol for the first time its class will add and if I click on the symbol a second time its class will be removed

e.t.c

It working but only on 1.st element

Ploblem is if i click on the first, second, third or fourth symbol it always changes the first one

<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
        <link rel="stylesheet"href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..45,100..700,0..1,-50..200" />
        <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" rel="stylesheet" />
    
        <link rel="stylesheet" href="style.css">
    
        <script src="script.js"></script>
    
    </head>
    <body>
        <Section class="s1">Smartphone <span class="material-symbols-outlined"> <span id="id1" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s2">Laptop <span class="material-symbols-outlined"> <span id="id1" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s3">Keyboard <span class="material-symbols-outlined"> <span id="id1" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s4">Monitor <span class="material-symbols-outlined"> <span id="id1" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
    
    
    </body>
    
    </html>
.favorite{
    color:black;
    font-weight: 1000;
}
.favorite:hover{
    color:red;
        
}
.fill{
    font-variation-settings:
    'FILL' 1;
}

var i=0;
function favourite(elmnt){
    i++;
    var test = document.getElementById(elmnt.id);
    if (i == 1) {
        test.style.color = "red";  
        test.classList.add('fill'); //add fill in class
        
    }
    
    if (i == 2) {
        test.style.color = "black";  
        test.classList.remove('fill'); //remove fill in class

        i =0;
    }
}

>Solution :

This part of the code var test = document.getElementById(elmnt.id); is unnecesary, you already passed the element with your favourite(this) function.

let i=0;
function favourite(elmnt){
    i++;//increment
    if (i == 1) {
        elmnt.style.color = "red";  
        elmnt.classList.add('fill'); //add fill in class
    }
    if (i == 2) {
        elmnt.style.color = "black";  
        elmnt.classList.remove('fill'); //remove fill in class
        i = 0;//resets
    }
}
.favorite{
    color:black;
    font-weight: 1000;
}
.favorite:hover{
    color:red;
        
}
.fill{
    font-variation-settings:
    'FILL' 1;
}
<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
        <link rel="stylesheet"href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..45,100..700,0..1,-50..200" />
        <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" rel="stylesheet" />
    
        <link rel="stylesheet" href="style.css">
    
        <script src="script.js"></script>
    
    </head>
    <body>
        <Section class="s1">Smartphone <span class="material-symbols-outlined"> <span id="id1" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s2">Laptop <span class="material-symbols-outlined"> <span id="id2" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s3">Keyboard <span class="material-symbols-outlined"> <span id="id3" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
        <Section class="s4">Monitor <span class="material-symbols-outlined"> <span id="id4" class="favorite" onclick="favourite(this);">favorite </span></span></Section>
    
    
    </body>
    
    </html>

Leave a Reply