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 7981691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:27:05+00:00 2026-06-04T10:27:05+00:00

I have a simple button in HTML like this: <input type=’button’ id=’btnPrint’ value=’Print’ onclick=changeStylesheet();

  • 0

I have a simple button in HTML like this:

<input type='button' id='btnPrint' value='Print' onclick="changeStylesheet();" />

This button already contains a onclick event handler. What I m trying to achieve here is to be able compose the onclick event handler for same the button. I would like to add second method that tracks how many times the button was hit.

some approaches:

First: Append another function call in HTML as per inline event registration.

<input type='button' id='btnPrint' value='Print' onclick='changeStylesheet();trackThis();>' />

This is the easiest way to go but not feasible in my situation.

Second: Grab the onclick event and check if it is undefined. Then, recompose the event handler with anonymous function using JavaScript native syntax.

var elem = document.getElementById('btnPrint');
var existingHandler = **elem.onclick ? elem.onclick : function () {};**
elem.onclick = function () {
     trackThis();
     existingHandler();
};

Third: Though above approaches work, I m curious to learn the way to do above #2 approach but with jQuery syntax.

 var elem = $('#btnPrint');
    var existingHandler = **elem.click ? elem.click : function () {};**
    elem.click(function () {
        trackThis();
        existingHandler()
    });

What is happening with #3 solution in mine is that I m getting
Uncaught TypeError: Object [object Window] has no method ‘trigger’
error.

Any hints will be very appreciated.

  • 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-04T10:27:07+00:00Added an answer on June 4, 2026 at 10:27 am

    If both handlers are to be attached in jQuery then simply set two click handlers. Internally, jQuery implements an “observer pattern” such that it aggregates handlers and will execute them sequentially in the order they were added.

    $('#btnPrint').on('click', firstHandler);
    $('#btnPrint').on('click', secondHandler);
    

    These statements need not be consecutive or even in the same scope.

    The same principle applies if a handler is already specified in the HTML. jQuery will simply execute any additional handlers after the original.

    HTML:

    <input type='button' id='btnPrint' value='Print' onclick=".....;" />
    

    jQuery:

    $('#btnPrint').on('click', secondHandler);
    

    See fiddle

    EDIT …

    It is also possible to revere the order such that the original handler executes after a handler attached in jQuery :

    var $btnPrint = $('#btnPrint');
    var originalHandler = $btnPrint.get(0).onclick;
    $btnPrint.get(0).onclick = null;
    
    $btnPrint.on('click', secondHandler);
    $btnPrint.on('click', originalHandler);
    

    See fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form like this <table> <tr><td >Topic: </td> <td> <input
i have this simple html form: <form action=test/ method=get accept-charset=utf-8> <input type=text name=first value=
i have this simple html form: <form action=test/ method=get accept-charset=utf-8> <input type=text name=first value=
I have HTML like this: <div id='main'> <div id='child-0'> <input type='text' name='data[0][name]'> <input type='text'
I have a simple form which is like this: The HTML: <form action=post> <input
This is probably a basic html/css question... I have a simple one-button form that
I hope the following isn't too tricky. I have a simple html button. Now
I have a simple Button control that contains an Image object as its content.
I have a simple button (3 of them - all same except the content
I have a simple submit button. I am wanting to align it to the

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.