Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
how do i put pics in classlist.remove/add in JS

Im new to stackoverflow and i started learning js just few weeks ago, heres the code:

 'use strict'; 
 let a = 1;
 king.addEventListener('click', function(){
     a++;
     if(a%2 == 0){
         king.classList.remove(?)
         king.classList.add(?)
     }
     else {
         king.classList.remove(?)
         king.classList.add(?)
 
     }
 });

a -> counter, king is a div, i have two pics named king0 and king1 in pics folder.

Task = when king is clicked, it switches to the second pic, -> first pic and so on

question = how do i put pics in "?", although probably i may did everything wrong and have to start all over

well html was requested but its a very basic one:

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "UTF-8">  
        <title>DOC</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id = "king" class = "kingcss"> //kingcss class is empty
        <img src= "pics/king0.png"> </div>
        <script src="script.js"></script>
    
    </body>  
</html>
 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

classList doesn't contain your images directly; instead it's a list of CSS class names applied to the element. You can associate images with those classes in CSS:

let a = 0;
let king = document.querySelector('.king')

king.addEventListener('click', function() {
  a++;
  if (a % 2 == 0) {
    king.classList.add('one')
    king.classList.remove('two')
  } else {
    king.classList.add('two')
    king.classList.remove('one')
  }
});
.king {
  width: 100px;
  height: 100px;
  border: 2px solid;
}

.one {
   background-image: url('http://placekitten.com/101/101')
}

.two {
   background-image: url('//placekitten.com/100/100')
}
<div class="king">King</div>

I'm going to suggest a few simplifications you could make to your code beyond just getting it working though:

  • You're depending on a global variable a here to keep track of which image you want to switch to next. This isn't necessary, and gives you one more thing to keep track of -- instead it'd be better to just check the element's current state.
  • You toggle one class on and another class off, which means you actually have four possible states (first one on, second one on, both on, or both off) if things don't go perfectly. (You can actually see this in the above example; before the user clicks it's in the "both off" state). You really only want two possible states: image one, or image two.

Below is an example with those issues resolved: instead of testing a you test the classList contents; and instead of having multiple classes to toggle, one of the images is in the "default" state.

let king = document.querySelector('.king')

king.addEventListener('click', function() {
    king.classList.toggle('two')
});
.king {
  width: 100px;
  height: 100px;
  border: 2px solid;
  background-image: url('http://placekitten.com/101/101')
}

.two {
  background-image: url('//placekitten.com/100/100')
}
<div class="king">King</div>

about 4 years ago · Juan Pablo Isaza Report

0

Please add your HTML. Not everything is wrong. W/o looking at your HTML, I'm assuming you have a different image tag for king0 and king1. If you want to use classList.remove() or classList.add(), you have to specify the class name you are adding or removing from the classList node.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!