Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!