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

158
Visualizações
Change element backgroundImage using Javascript and image import

I am building an app in React and am making a settings element. The element is a button that changes size on-click; which I have already achieved:


import SettingsA from "./images/settingsA.png";
import SettingsB from "./images/settingsB.png";

var settingActivated = 0;

function settingFunc() {
    settingActivated++;

    if (settingActivated%2===0) {
        document.getElementById("settingBut").setAttribute("style", "width: 6rem; background-image: {SettingsA}");
            document.getElementById("fontBut").setAttribute("style", "width: 0rem; height: 4rem;");
    } else {
        document.getElementById("settingBut").setAttribute("style", "width: 10rem; background");
            document.getElementById("fontBut").setAttribute("style", "width: 7rem; height: 4rem;");
    }
  }

<div id="settings">
     <button id="settingBut" onClick={settingFunc}></button>
          <div id="settingDropdown">
               <button id="fontBut" onClick={legacyFont}>Legacy<br/>Font</button>
</div>

But what I want to do is make it so the setting icon displayed on the button can change on-click as well. Please help me out. Specifically: the image is passed to the button via css background-image, & the css is passed to the button via on-click function using .setAttribute; I am fine with setting the image via path or import variable, I just want it to work. settingFunc and legacyFont functions are not provided in my code, everything involved with my prompt is provided.

about 4 years ago · Juan Pablo Isaza
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