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

181
Visualizações
JavaScript change image and text when hovered using mouseover and eventlistener

I am currently trying to make an image replace another in a fixed position while also changing the color style of the text at the same time. The image and the text needs to change when one or the other is being hovered over by the user. I have the image and text in a div and attempted to use an eventlistener to change the objects.

HTML:

<div id="dequestimg">
    <p style="font-size: 75%; float: left; font-weight: bold">Delete Requests:</p>
    <img src="dequestbutton.png"/>
</div>

JavaScript:

document.addeventlistener("mouseover", mouseover);
document.addeventlistener("mouseout", mouseout);

        function mouseover() {
            document.getelementbyid("dequestimg").style.color = "red";
            document.getelementbyid("dequestimg").getelementsbytagname("img") = "dequestbuttonhover.png";
        }

        function mouseout() {kossa
            document.getelementbyid("dequestimg").style.color = "black";
            document.getelementbyid("dequestimg").getelementsbytagname("img") = "dequestbutton.png";
        }

I am new to JavaScript so any help would be appreciated.

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

0

Many points here

  • You have an unexpected string in your JavaScript making it invalid: kossa
  • JavaScript is case-sensitive, you have to use the correct casing in the function names: getElementById, getElementsByTagName
  • You can define a variable at the top to select your div
  • You should probably give the img element an id as well so you can select it separately
  • You need to set the src property of the image once it's selected, you can't just assign it as a string

Here's a working example

HTML index.html

<!DOCTYPE html>
<html>
  <body>
    <div id="dequestimg">
      <p style="font-size: 75%; float: left; font-weight: bold">
        Delete Requests:
      </p>
      <img id="image" src="dequestbutton.png" />
    </div>
    <script src="index.js"></script>
  </body>
</html>

JavaScript index.js

const div = document.getElementById("dequestimg");
const img = document.getElementById("image");

document.addEventListener("mouseover", mouseover);
document.addEventListener("mouseout", mouseout);

function mouseover() {
  div.style.color = "red";
  img.src = "dequestbuttonhover.png";
}

function mouseout() {
  div.style.color = "black";
  img.src = "dequestbutton.png";
}
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