Using JavaScript Event ListenersšŸ‘‚and why JavaScript isnā€™t all bad

Matthew Thorry
Frontend Weekly
Published in
5 min readSep 5, 2017

--

Learning JavaScript can be a challenging task. Iā€™m currently learning JavaScript after spending six glorious weeks at Flatiron School working with Ruby. Ruby was a simple and clear language ā€” JavaScript is much more verbose and takes a lot more lines of code to produce the same thing in Ruby. This has caused JavaScript to have many haters.

While I was among the haters, Iā€™m slowly coming around to JS. Also it runs the web soooo I really need to get on board.

One important aspect of JavaScript is making the page dynamic without reloading. In a previous project that I worked in using Rails, we implemented a lot of the same functionality that once can implement using EventListeners in JavaScript. This represents one time where JavaScript is actually more concise than Ruby so letā€™s see how it works!

My Ruby project, which I created with hui wang, is a social events app. In it, users can add/delete events that they are attending. They can also follow/unfollow other users as well as have followers. To add and delete events from a userā€™s page, there were multiple steps involved.

Using Ruby

Screenshots from the Events app I created. Left is some of the code and the right is the actual page.

In my EventsApp (working title: ā€œLetā€™s Have Fun Togetherā€ and can be shortened to ā€œLetā€™sā€ for the hip people. I imagine users telling their friends that they found the event on ā€œLetā€™sā€. ā€œHey, howā€™d you hear about this?ā€ ā€œLetā€™s!ā€) a user can add and remove events from ā€œtheir eventsā€. You can see in the screenshot the ā€œRemoveā€ button under each event. On pages that have events you are not attending, you would see ā€œAdd to my eventsā€.

To make this happen, hereā€™s what we had to do:

  • Create add/remove events actions in the controller
  • Add routes for the controller actions
  • Create class methods that define what will happen in controller actions
  • Update views to include buttons that access above actions

This was a lot of work and left much room for error.

In the screenshot here, I copied the lines of code that were specific to our add/remove events. As you can see, it took 37 lines of code in two controllers, one class and almost every view. That isnā€™t very DRY.

Using Javascript

In this project, I was creating a Task Manager app with Roman Mullady where a user could create To Do lists with various tasks. The user could also delete their entire To Do List or specific items on the list. This project is still in progress so apologies for the lack of formatting. Itā€™s ā€œearly internet chicā€ which is totally in right now. Seriously.

But I digress. For this project, to add/delete lists we had to:

  • Create constants for the elements we were selecting (not really a ā€˜requiredā€™ step)
  • Add event listeners to the buttons
  • Create functions that fire when buttons are clicked

Much less steps! Also, even with the extra steps of setting constants, we only wrote 26 lines of code. JavaScript required less code than Ruby!

Letā€™s break it down.

In JavaScript, event listeners are basically functions that are waiting around for someone to come along and trigger them. In the example I provided above, the event they were listening for was a userā€™s click on one of the specified buttons. I also had event listeners for the specific tasks but I only included those for deleting a list to keep it simple.

By adding event listeners to my JavaScript code, I was able to accomplish the same goal as my Ruby code in a much more concise way. While my Ruby application was doing quite a bit more (making API calls, storing to the database, etc) I feel that I could implement JavaScript event listeners to simplify that code.

Also by using event listeners, I can change a lot more about my current code without having to go through all the different routes required by my Ruby project. In all honestly, I had to do a bit of hunting through my code to find everything related to my add and delete event actions (and itā€™s entirely possible thereā€™s some more code, like my routes, that I didnā€™t include in my example). Thatā€™s not good.

In summary: JavaScript is a wordy language that can be quite difficult but it has much more functionality than Ruby and allows for a more dynamic page with less headache.

More reading about Event Listeners:

EventListener ā€” Web APIs | MDN

JavaScript DOM EventListener ā€” W3 Schools (I personally prefer W3 to MDN because W3 allows you to play around with the code using real examples)

If you enjoyed the article, Iā€™d love for you to let me know by giving a clap! You can also see my code on GitHub or learn more about me professionally on LinkedIn

--

--

Matthew Thorry
Frontend Weekly

Former science teacher šŸ‘ØšŸ½ā€šŸ« turned software engineer šŸ‘ØšŸ½ā€šŸ’» More at: thorry.io