Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Can you change out an image in CSS using JavaScript

I'm currently creating an online quiz with HTML, JS, & CSS.

I have created question boxes that cycle through each question, but I am trying to have a different image displayed for each new question.

I currently have a CSS element that can place an image into the quiz box but doing that means I have the same image for every question.

I was hoping to use JS to change the image but I haven't quite got it yet.

.styledimg {
    background-image: url(spider.png);
    background-repeat: no-repeat;
    width: 640px;
    height: 360px;
}

Here I have the image contained in the CSS element

let questions = [
    {
    numb: 1,
    question: "Question",
    answer: "option", 
    options: [
      "option",
      "option",
      "option",
      "option"
    ]
  },

Here is the start of the array that houses my questions

 <div class="styledimg"></div>

Here is the tag associated with the CSS element


I was trying to use something like 'document.querySelector("styledimg").style.background-image = "clock.png"'

to change the image for each question but to no avail.

If anyone could point me in the right direction that would be appreciated.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try

document.querySelector(".styledimg").style.backgroundImage = "clock.png"

You can also do this by adding a class to this element:

.styledimg {
    background-image: url(spider.png);
}

.styledimg.clock {
    background-image: url(clock.png);
} 

document.querySelector(".styledimg").classList.add('clock');

about 4 years ago · Santiago Trujillo Relatório

0

It would be nice to have a working snippet. (HTML+CSS+JS)

Other than that, you should simply create an <img id="quizzImg" src="spider.png"> tag into the <div>.

After that, simply:

const quizzImage = document.querySelector("#quizzImg")
quizzImage.setAttribute("src", "clock.png")

You would just have to wrap that into a function, and call it whenever you change of question, pass as argument the new image file name, and you're set and it will be easy to use on your end.

const quizzImage = document.querySelector("#quizzImg")

function setQuizzImage(src) {
    quizzImage.setAttribute("src", src)
}

Done, you have a function that can be used again and again to change the image by anything you want, you just have to call that function and pass the file name as an argument like:

setQuizzImage("clock.png")

All that is left is to make the condition over it to determines when you will run it, and what image you will set based on that condition.

As we have no idea of your code structure and process, I will try to guess. If you call a function to change the question, you could store the question number in a variable, and based on that number, make usage of a if statement in something like that:

function changeQuestion() {
    [...]
    if (currentQuestion == 1) {
        setQuizzImage("question1.png")
    } else if (currentQuestion == 2) {
        setQuizzImage("question2.png")
    } else
    [...]
}

You could imagine setting it here. (of course, I had to hardcode the if as I have no idea of how your code works)

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda