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

68
Vistas
how to show image on click, and hide it again when clicking again

I want to when I click on a sentence or a button an image appears, and when I click it again it disappears. but my code doesn't seem to work using JS and HTML.

let pic = document.getElementById("hiddenclickimg");
let word = document.getElementById("hiddenclick");

function showPic(){
  pic.hidden = 'false' ;
  word.style.color = 'red';
  word.style.backgroundColor = 'blue';
}

word.onclick = showPic;
<h3 id="Work">Work Experience</h3>
      <div class="work">
        <ul>
          <li>
            <img
              src="./Images/Alex Sydney.jpg"
              alt="alex sydney"
              id="hiddenclickimg"
              hidden="true"
            />
            <button id="hiddenclick">Alex Sydney Hospital</button>
          </li>
        </ul>
      </div>

<script src="./main.js"></script>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use the onclick of a button and toggle a class that hides the image when it has that class.

HTML:

<button onclick="imageClick()">Toggle Image</button>

CSS:

.hidden {
  opacity: 0;
}

Javascript:

const image = document.getElementById('hiddenclickimg');

const imageClick = () => {
   image.classList.toggle('hidden');
}

There are different CSS properties you could use to hide the image, but opacity is a good basic one.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need to set 2 ids to do the trick using css property display none.

For example you want to hide that image:

<img id="dog_image" src="dog.png" alt="Rex the Labradoodle" >

function hideDog() {
  const dog = document.getElementById("dog_image");
  if (dog.style.display === "none") {
    dog.style.display = "block";
  } else {
    dog.style.display = "none";
  }
}

And then just set the button:

<button onclick="hideDog()">Hide/Show my dog</button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use flag to change state of the image like following

let  imagehidden =false
let pic = document.getElementById('hiddenclickimg');
let word = document.getElementById('hiddenclick');

function showPic(){
if(imagehidden){
  pic.hidden = false ;
  word.style.color = 'red';
  word.style.backgroundColor = 'blue';
  imagehidden=false
  }
  else{
   pic.hidden =true;
   imagehidden=true
  }
}

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