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

195
Views
javascript onclick css fade in each time, not just 1st time

How can I have text fade in on each click and not just the first time using css transition and JavaScript?

Here is what I have so far

<style>
#data {
    transition: .7s;
}
</style>

<h1 id="data"></h1>
<a href="#" id="clicker">click 1</a>

document.getElementById('data').style.opacity = 0;
function go(event){
    event.preventDefault();
    document.getElementById('data').style.opacity = 1;
    document.getElementById('data').innerHTML = 'test';
}
document.getElementById('clicker').addEventListener('click', go);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It can also be done using Element.animate():

const dataElement = document.getElementById('data')
dataElement.style.opacity = 0

function go(event) {
  event.preventDefault()
  dataElement.animate({
    opacity: [0, 1]
  }, 700).onfinish = () => dataElement.style.opacity = 1
}
document.getElementById('clicker').addEventListener('click', go)
<h1 class="fade-in" id="data">test</h1>
<a href="#" id="clicker">click 1</a>

EDIT: In the above snippet onfinish event handler was used to maintain the final opacity value since it was being set back to 0 after the animation ends. But I found that this can also be achieved by setting fill: 'forwards' in the keyframe options:

const dataElement = document.getElementById('data')
dataElement.style.opacity = 0

function go(event) {
  event.preventDefault()
  dataElement.animate({
    opacity: [0, 1]
  }, {
    duration: 700,
    fill: 'forwards'
  })
}
document.getElementById('clicker').addEventListener('click', go)
<h1 class="fade-in" id="data">test</h1>
<a href="#" id="clicker">click 1</a>

Also you might want to check browser compatibility before implementing those approaches

And if you want a safer approach you may use css animations:

const data = document.getElementById('data')
data.style.opacity = 0
const clicker = document.getElementById('clicker')
clicker.addEventListener('click', () => {
  data.classList.remove('fade-in')
  data.offsetWidth // required to trigger a reflow and restart the animation
  data.classList.add('fade-in')
})
.fade-in {
  animation-name: fadein-animation;
  animation-duration: 700ms;
  animation-fill-mode: forwards;
}

@keyframes fadein-animation {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<h1 id="data">test</h1>
<a href="#" id="clicker">click 1</a>

about 4 years ago · Juan Pablo Isaza Report

0

setTimeout might work for you. On click set it invisible immediately and use setTimeout to have a delay then show it again.

document.getElementById('data').style.opacity = 0;
document.getElementById('data').innerHTML = 'test';
function go(event){
    event.preventDefault();
    document.getElementById('data').style.opacity = 0;
    setTimeout(() => {
      document.getElementById('data').style.opacity = 1;
    }, 1000);
}
document.getElementById('clicker').addEventListener('click', go);
#data {
    transition: .7s;
}
<h1 id="data"></h1>
<a href="#" id="clicker">click 1</a>

about 4 years ago · Juan Pablo Isaza Report

0

<h1 id="data" style="opacity:0">&nbsp;</h1>
<button type="button" id="clicker">Fade In</button> 
<script>
var data = document.getElementById('data');
function fadeIn(){
    data.innerHTML = 'Data entered successfully.';
    data.animate({opacity:[0,1]},{duration:400,fill:'forwards'});
}
document.getElementById('clicker').addEventListener('click',fadeIn);
</script>

only IE doesn't not support animate

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!