Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:12+00:00 2026-06-15T15:22:12+00:00

So I have a regular onclick event attached to a few buttons, each function

  • 0

So I have a regular onclick event attached to a few buttons, each function that handles the onclick event does something different (so I can’t reuse the same function for both events).

element1.onclick = function() {
    if(this.classList.contains('disabled') {
        return false;
    }
    // For example make an AJAX call
};

element2.onclick = function() {
    if(this.classList.contains('disabled') {
        return false;
    }
    // For example hide a div
};

I’m writing duplicate code for this ‘disabled’ class check, I want to eliminate this by hooking in some common onclick check then fire the regular onclick event if that check passes.

I know the below won’t work but I think it will illustrate what I’m trying to do:

document.addEventListener('click', function() {
    // 1. Do the disabled check here
    // 2. If the check passes delegate the event to the proper element it was invoked on
    // 3. Otherwise kill the event here
});

I’m not using any JavaScript library and I don’t plan to, in case someone comes up with ‘Just use jQuery’ type answers.

EDIT: Had to pass boolean third argument to addEventListener as true and everything is fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:22:13+00:00Added an answer on June 15, 2026 at 3:22 pm

    Use event capturing, like so:

    document.addEventListener('click', function(event) {
        if (/* your disabled check here */) {
          // Kill the event
          event.preventDefault();
          event.stopPropagation();
        }
    
        // Doing nothing in this method lets the event proceed as normal
      },
      true  // Enable event capturing!
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an area that has an onclick event. When clicked on a
I have the following applied to a string: function onclick(event) { showOverlay('overlayWrapper', 'overlayIframe', 'http://www.bestinvest.co.uk');
I have the following: $(#article).on('click', 'a[href^=/C], a[href^=/Java], a[href^=/T]', function (event) { Can this be
I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
I have an onclick function which performs several tasks. In another JavaScript function I
Consider i have 3 linkbuttons on a page, <asp:LinkButton ID=LB1 runat=server CssClass=regular OnClick=LB1_Click> Today
I am have buttons like this: <button type=button class=img img_button_bla onclick=...>Bla!</button> The img class
Hey i have the following statechange event set on the window : History.Adapter.bind(window,'statechange',function(e){ console.log(statechange
I have this script uses regular expressions to check that a form field contains
I have a div with some id that contains some buttons/labels. Its a container

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.