Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Should I add event listeners in component class?

I'm using Vanilla JavaScript Modules. I have main.js file (where I start the app) and other component files (hamburger.js, modal.js etc.) These components have elements I want to listen but I don't know if I should add their event listeners in their own class or I should add them in main js.

Event Listeners in Class Example

export default class Hamburger {
  constructor(element) {
    this.element = element;
  }
  handleClick() {
    // code...
  }
  addEventListeners() {
    this.element.addEventListener('click', this.handleClick);
  }
}

Event Listeners in Main JavaScript File Example

import Hamburger from './hamburger.js';

const element = document.querySelector('.js-element');

const hamburger = new Hamburger(element);

hamburger.element.addEventListener('click', handleClick);

These are the examples that come to my mind. If there's a better way without using any framework or library, I'm open to it.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!