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

151
Visualizações
Changing background of div in JavaScript

my code have one div and three picture my main task is to show the same image in div when I hover over any picture what I am not able to understand is how can I do it with variables so that if in future if add more pictures it does not make any problem for me note:: I want to do it by using variables I don't want to hard code image source. I am also attaching the image for reference only.

function showproperties(element) {
  var x = document.getElementById("box001");
  x.innerHTML = element.alt;
  var y = element.src;
  document.getElementById("printsrc").innerHTML = y;
  x.style.backgroundImage = y;
}
<body>
  <div id="box001">main hover box

  </div>
  <div class="row">
    <div class="image001 imgresize">
      <img src="img01.jpg" alt="Image 001" onmouseover="showproperties(this)" onmouseleave="defaulttext(this)">
    </div>
    <div class="image002 imgresize">
      <img src="img02.jpg" alt="image 002" onmouseover="showproperties(this)" onmouseleave="defaulttext(this)">

    </div>
  </div>

</body>

image

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I tried to reproduce your issue and made some changes in it. I Hope that's how you wanted it to work. I have used some gif images url for demo purpose.

HTML:

       <div id="box001" style="width: 100px;height: 100px; margin:7px">main hover box
      
        </div>
        <div class="row">
          <div class="image001 imgresize">
            <img src="https://c.tenor.com/Lvhj4QVL8-4AAAAS/server-is-for-javascript.gif" alt="Image 001" onmouseover="showproperties(this)" onmouseleave="defaulttext(this)">
          </div>
          <div class="image002 imgresize">
            <img src="https://c.tenor.com/FIcvxhgc1sgAAAAS/dit-is-een-code-blok-code.gif" alt="image 002" onmouseover="showproperties(this)" onmouseleave="defaulttext(this)">
      
          </div>
        </div>
      

JS:

var x = document.getElementById("box001");

function showproperties(element) {
  var y = element.src;
  x.innerText = "";
  x.style.backgroundImage = `url(${y})`;
}

function defaulttext(element) {
  x.innerText = "main hover box";
  x.style.backgroundImage = "none";
}

Full working code is available on this fiddle - https://jsfiddle.net/0h1urctn/

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