I have set a toggle event in JavaScript, but when the page loads I have to click twice on the element for it to fire. After that it responds normally to just one click but as soon as I refresh the page it happens again.
This is where it happens:
This is the website and you can clearly see the behavior yourself: https://n-ii-ma.github.io/Portfolio-Website/
This is the JS code for the event:
/* Read More */
let readMore1 = document.getElementById('show-card-1');
let readMore2 = document.getElementById('show-card-2');
let readMore3 = document.getElementById('show-card-3');
/* Projects Description */
let desc1 = document.getElementById('details1');
let desc2 = document.getElementById('details2');
let desc3 = document.getElementById('details3');
/* Toggle Description */
readMore1.onclick = function() {
if (desc1.style.display === 'none') {
desc1.style.display = 'block';
readMore1.style.color = 'DeepPink';
readMore1.innerText = 'Read Less';
}
else {
desc1.style.display = 'none';
readMore1.style.color = '';
readMore1.innerText = 'Read More';
}
}
readMore2.onclick = function() {
if (desc2.style.display === 'none') {
desc2.style.display = 'block';
readMore2.style.color = 'DeepPink';
readMore2.innerText = 'Read Less';
}
else {
desc2.style.display = 'none';
readMore2.style.color = '';
readMore2.innerText = 'Read More';
}
}
readMore3.onclick = function() {
if (desc3.style.display === 'none') {
desc3.style.display = 'block';
readMore3.style.color = 'DeepPink';
readMore3.innerText = 'Read Less';
}
else {
desc3.style.display = 'none';
readMore3.style.color = '';
readMore3.innerText = 'Read More';
}
}
Do you know how I can solve this issue?