Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda