How are events handled in JavaScript?
Events in JavaScript are essential for interacting with users and responding to their actions. One of the most common events is the 'click' event. This event is essential in any interaction that requires opening popup windows or 'modals'. Here we will see how to bind a click event to a button to manage the opening of a modal.
How to implement a click event?
To implement the click event, you need to associate a function that will be executed when a button is clicked. You do this with the addEventListener
method. In the context of our example, we are going to associate the button with a function on click, modifying CSS classes to display the modal.
Button.addEventListener('click', () => { modal.classList.remove('hidden'); modal.classList.add('visible');});
This code snippet removes the 'hidden'
class and adds the 'visible'
class, thus making the modal display. The 'hidden'
and 'visible'
classes control the visibility of the modal by managing style properties such as display
and opacity
.
How to manage the visibility of the modal?
To ensure that the modal has the desired appearance and is positioned correctly on the screen, it is crucial to work with the CSS classes. This is where display: grid
comes in handy, as it allows you to center the elements inside the modal.
.modal-container { background: white; width: 50%; height: 50%; display: grid; place-items: center;}
The place-items: center;
function centers the content horizontally and vertically, ensuring that your modal is aesthetically pleasing in the interface.
How to close the modal?
Closing the modal is a similar process to the one we use to open it. We can include a closing icon (for example, an "x") and associate it with a click event that hides the modal. This is where using icons8.com
comes in to get free icons that can be integrated into your project.
Adding an icon to close the modal
The closing icon is obtained and included in the HTML. Using the following code, we associate a click event that hides the modal by removing the 'visible'
class and adding 'hidden'
.
closeButton.addEventListener('click', () => { modal.classList.add('hidden'); modal.classList.remove('visible');});
Considerations about specificity and the use of !important
When modifying classes to control visibility, a CSS specificity issue can arise. Sometimes it is necessary to use !important
to ensure that the rules are applied independently of the other classes.
.hidden { display: none !important;}.visible { display: grid;}
Although !important
is not always good practice, in some cases, such as this one, it is useful to overcome specificity issues.
Challenge: Practicing with other elements
A great way to solidify your understanding is to practice associating click events with other buttons or elements on the same page. Try replicating this modal behavior with different superheroes, creating separate modals for each of them.
Don't forget to explore and experiment with design and functionality variations - keep practicing and improving your JavaScript skills to create dynamic and engaging user interfaces!
Want to see more contributions, questions and answers from the community?