I did this successfully for an element id; gave it the 'click' event listener so a modal can pop up once the action takes place. Now, I am trying with the same JS code but targeting a class instead of the id and nothing is popping up, the link just takes me to the site directly instead of making the modal appear.
It is worth saying that my id element is a navbar button, and my class element is just text with the <a href=
element and class= "link-to-app"
. Here is the code for the navbar button, which is successfully running:
<script>
//Client Login: Redirect Modal
document.querySelector('.redirect-modal').style.display = 'none';
document.getElementById('btn-nav').addEventListener ('click', function() {
document.querySelector ('.redirect-modal').style.display = 'flex';
document.querySelector ("body").style.overflow = 'hidden';
});
//Redirect Modal: Close
document.querySelector('.close').addEventListener ('click', function() {
document.querySelector ('.redirect-modal').style.display = 'none';
document.querySelector ("body").style.overflow = 'visible';
});
</script>
Then, the code for the class element, which as I already said, is not working:
<script>
document.getElementsByClassName('.link-to-app').addEventListener('click', function() {
document.querySelector('.redirect-modal').style.display = 'flex';
document.querySelector("body").style.overflow = 'hidden';
})
</script>
I don't know if using an array with the forEach method would help, and in such case, I don't know if I need to delete the code for the ID and include this one on the array along with the class. Any help would be great, I have tried multiple methods but nothing seems to work properly. Thank you in advance.