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

149
Visualizações
need help removing an IMG and replacing it with text in its position with javascript

Im working on a project and a part of it is making an image disapear on hover, and replace that with text in the same location! I have to do it through javascript. im very new to front end web development so any help would be great!

.main-img1{
    height: 400px;
    width: 600px;
    margin-top: 80px;
    background-size: 600px 400px;
    box-shadow: 10px 10px 5px rgb(24, 22, 22);
    position: relative;
    
   text-align: center;
   font-size: 25px;
   color: black;
   border-radius: 50px;
}


.img1-text{
    display: none;
    position: absolute;
   bottom: 8px;
   left: 150px;
 
<section class="main-body">
       <div>
         <img class="main-img1" src="img/automotive.jpg">
          
          <h1 class="img1-text" id="img1text"> Here are some samples of my automotive photography! I specialize in "Rolling Shots" which are caputring a vehicle in motion, while the background and foreground show the motion.</h1>
       </div>

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

0

You can replace any element using the "magical" outerHTML like this...

First, I gave your image an ID to make javascript operations easier...

<img id="I" class="main-img1" src="img/automotive.jpg">

Now replace the image with a paragraph of text...

I.outerHTML='<p>Well what do you know!</p>';

For easy one-line HTML...

<img onmouseover="this.outerHTML='<p>Well what do you know!</p>';" class="main-img1" src="img/automotive.jpg">
about 4 years ago · Juan Pablo Isaza Relatório

0

First off, this is a very odd thing to do in Javascript. Usually hover states, appearing and disappearing, etc. are handled by CSS.

to do it in js you have to add a mouseover event listener to the image to execute a function to grab the element you want to disappear, add a css class to apply "display: none" to it, grab the element you want to appear and remove a class that adds "display: none" from it.

assuming you have a 'display-none' class on your text element that applies 'display: none' to it, you can do this:

const image = document.querySelector('.main-image1')
const text = document.querySelector('.img1-text')
image.addEventListener('mouseover', () => {
    image.classList.add('display-none')
    text.classList.remove('display-none')
}

if you were to do this with css its as simple as

.image {
    z-index: 2;
}
.image:hover {
    display: none;
}
.text {
    z-index: 1;
}

that way the text is set behind the image and when you hover over the image it disappears. This also has the benefit of when you take your cursor off the image for the image to reappear where js will need to be told explicitly to do that.

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