DOM events... lets just jump into it.
Tags: javascript, beginners, codenewbie, project
Published: 2020-10-12 08:16:29

Before you dig deep into this, Let me explain how things will go down real quick.  I will be talking about how everything went for me creating this project, then will get a bit too techy on you and talk about one particular issue that almost made me want to burn my laptop to the ground.
<br>
I will not explain everything I did, but if you are interested, I will link the repository at the end also a video of my project, to give you a better idea.
<br>
Let me take you back to a few months ago when I decided to code 5, projects out of my comfort zone to get a feel for JavaScript because, as we all know, it’s a hell of a beast to tackle, and boy, was I afraid to experiment with it and let me tell you I was dealing with so much self-doubt.
<br>
After looking around for ideas, I remembered I always wanted to try carousels, Although all the articles I read told me it was a bad idea since it is hard to make responsive and so many other reasons but I love a good challenge, so I went for it. 
<br>
Let's fast-forward, I found a design that I liked and tweaked it a bit and started coding, and it was a smooth sailing well....
<br>
Until the JavaScript part started especially, DOM events and all hell broke loose.

<br>

I struggled a lot with everything and was feeling like a lost kid in a mall. Until I gave up and reminded myself **asking for help is perfectly okay**, and some amazing people helped me out get a basic idea of how to deal with DOM event. Therefore, got over my fear to some extent. 
<br>

Now let a newbie get a bit techy on you.

![alt](https://lh3.googleusercontent.com/proxy/g9i3TkRfYgNXUbQC2wKDqaJALEpgxe5iQbsyCcNWjaHkzHe9OwHTtaZhmXpNP9i_Khg4Ti4wYP95jjI3_EkfH4XuD6nPsahETv-WwzkLIc96xTkLiZJam2aSqy6zKZ_RNKzyNMVZCiTrT6lpTdu5HH1MxugigWH9Kb8)


When you click on a carousel item, it will add a CSS class that would expand to full size, show articles content (a close button and some extra text, etc.…).
<br>
By clicking on the close button, The class will be removed, but that did not happen because of the event bubbling up and hitting all its ancestor elements, and that would include the carousel item resulting in re-adding the class immediately.
<br>

It was an invisible issue until I caught it using the debugger.
<br>

That’s called event bubbling and honestly, I was scratching my head like wtf is that, why was it happening and how to prevent it in the future well just code a modal pop up instead of this little hack, and maybe, things will not get this complicated, but I digress.
<br>
###What is event bubbling?

When an event is triggered on an element, it first runs the handlers on it then that same event will trigger across the same element's ancestors.
<br>

###How to fix it ?

You need to add preventDefault and or stopPropagation into the event handlers. this stops the browser from letting the click event progress passed the event handler and hitting its parent element.
<br>
###what's the difference between preventDefault and stopPropagation?

####preventDefault:
A method that prevents the default behavior of an element, for example, if we use a button for navigation, it will prevent it from being clicked or allow the user to leave the page.
<br>
####stopPropagation :
By default, all event handlers are registered in the bubbling phase. If you click on an element, the click event bubbles from the clicked element to the html element.
<br>
We need to use stopPropagation it will make it so that the first handler will run, but the event does not bubble any further up the chain, meaning no more handlers will be run.

<br>

___

#### We made it this far let's recap what I learned from this journey:
<br>

 * Asking for help does not mean you are less in anyway, in fact, it might give you a fresh perspective. 
<br>

 * Don't overcomplicate stuff and always take the easier route.
<br>

 * Always plan how to code something instead of jumping into it.

<br>
___
If you have any feedback or a suggestion about anything in this post or about how I can improve any upcoming articles. 

Feel free to comment below or contact me on <a href="https://twitter.com/MariamV_96"> Twitter!</a>

Thank you, for reading this far. I hope you learned a thing or two.
<br>

[Twitter Video](https://twitter.com/MariamV_96/status/1313329811058810880) | [GitHub repository](https://github.com/AfricanSoul/Evru-articles)

___
I want to give a shout out to everyone who helped me. 
check them out please, They have Amazing content.

[@ljc_dev](https://twitter.com/ljc_dev)
[@SpeakingSoftwa1](https://twitter.com/SpeakingSoftwa1)
[@inescodes](https://twitter.com/inescodes)
[@thechampsmoke](https://twitter.com/thechampsmoke)
[@L422Y](https://twitter.com/L422Y)