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

136
Vistas
Script to display opacity not working after next attempts

I wanted to create a notification alert when I click something. The first notification works but the succeeding do not show the notification at all. Here is the code:

'use strict';

window.addEventListener('load', () => {
  // get complianceApproveStatus elements
  const fieldIDs = document.querySelectorAll('.field-id');

  const copiedAlert = document.createElement("span");
  copiedAlert.id = 'tooltiptext';

  copiedAlert.innerHTML = 'Copied!';

  document.body.appendChild(copiedAlert);

  const displayAlert = () => {
    copiedAlert.style.opacity = 1;
  }

  fieldIDs.forEach(fieldID => {

    fieldID.addEventListener('click', () => {
      displayAlert();
      navigator.clipboard.writeText(fieldID.textContent);
    });
  });
});
.field-id {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
  margin: 5rem;
}

.field-id:hover {
  cursor: pointer;
}


/* Tooltip text */

#tooltiptext {
  opacity: 0;
  background-color: black;
  color: #fff;
  text-align: center;
  padding: 5px;
  border-radius: 6px;
  position: fixed;
  bottom: 20px;
  right: 70px;
}

#tooltiptext {
  -moz-animation: cssAnimation 0s ease-in 5s forwards;
  /* Firefox */
  -webkit-animation: cssAnimation 0s ease-in 5s forwards;
  /* Safari and Chrome */
  -o-animation: cssAnimation 0s ease-in 5s forwards;
  /* Opera */
  animation: cssAnimation 0s ease-in 5s forwards;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}

@keyframes cssAnimation {
  to {
    opacity: 0;
  }
}

@-webkit-keyframes cssAnimation {
  to {
    opacity: 0;
  }
}
<span class='field-id'>click 1</span>
<span class='field-id'>click 2</span>
<span class='field-id'>click 3</span>
<span class='field-id'>click 4</span>

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

0

Instead of using animations, you can just style the elements using JS and setTimeout.

'use strict';

window.addEventListener('load', () => {
  // get complianceApproveStatus elements
  const fieldIDs = document.querySelectorAll('.field-id');

  const copiedAlert = document.createElement("span");
  copiedAlert.id = 'tooltiptext';

  copiedAlert.innerHTML = 'Copied!';

  document.body.appendChild(copiedAlert);

  const displayAlert = () => {
    if (copiedAlert.style.opacity === "" || copiedAlert.style.opacity === "0") {
      copiedAlert.style.opacity = 1;
      setTimeout(() => copiedAlert.style.opacity = 0, 5000);
    }
  }

  fieldIDs.forEach(fieldID => {

    fieldID.addEventListener('click', () => {
      displayAlert();
      navigator.clipboard.writeText(fieldID.textContent);
    });
  });
});
.field-id {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
  margin: 5rem;
}

.field-id:hover {
  cursor: pointer;
}


/* Tooltip text */

#tooltiptext {
  opacity: 0;
  background-color: black;
  color: #fff;
  text-align: center;
  padding: 5px;
  border-radius: 6px;
  position: fixed;
  bottom: 20px;
  right: 70px;
}
<span class='field-id'>click 1</span>
<span class='field-id'>click 2</span>
<span class='field-id'>click 3</span>
<span class='field-id'>click 4</span>

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