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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:20:37+00:00 2026-06-05T19:20:37+00:00

I know what is the problem but I don’t really understand why it is

  • 0

I know what is the problem but I don’t really understand why it is happening. Suppose you have this:

HTML:

<div><p>Hello</p><button>Fire</button></div>
<br/>
<button id="menu-button">Menu</button>

JavaScript:

function myObject(container, buttonElement) {
    this.container = container;
}

myObject.prototype.change = function () {
    var box = this.container;
    console.log(box);
    box.find('button').on('click', function() {
        console.log('firing');
        box.find('p').toggle();
    });

};

var obj1 = new myObject($('div'));
$('#menu-button').on('click', function(){
    obj1.change();
});​

Fiddle: http://jsfiddle.net/L24As/1/

As you can see, when you click on the “Menu” button and obj1.change() starts, which adds an event handler to the button “Fire”, everything works as expected. However, if you click again on the “Menu” button, you are creating another event, so now toggle() doesn’t work because the first event is hiding and second one is showing the paragraph. Why is that? I would have thought that the event should have been overwritten.

I solved it like this:

http://jsfiddle.net/L24As/3/

function myObject(container) {
    this.container = container;
    this.change = function () {
        var box = this.container;
        console.log(box);
        box.find('button').on('click', function() {
            console.log('firing');
            box.find('p').toggle();
        });
    };
}

var obj1 = new myObject($('div'));
obj1.change();

​Is it a good solution? The event is attached to the button “Fire” since the start, so the “Menu” button is used to show other things, which makes them a bit different, although they perform the same task.

  • 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-05T19:20:40+00:00Added an answer on June 5, 2026 at 7:20 pm

    I would have thought that the event should have been overwritten.

    No, it wouldn’t be overwritten. It’s adding them and firing them all (see addEventListener which is behind the on function).

    You should add the event once. Your solution is correct.

    Here’s the essence of the problem (see the comments):

    myObject.prototype.change = function () {
        var box = this.container;
        console.log(box);
        box.find('button').on(
            'click',
    
            // Here you create a new function object.
            // Each time you call the `change` method,
            // it will add another function object as an event handler.
            function () {
                console.log('firing');
                box.find('p').toggle();
            }
        );
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem that I probably understand but don't know how to handle,
I have this problem but I don't know how to name it, for instance,
I have a simple problem but I don't know how to solve it because
My problem is very simple but I don't really know its name and therefore,
I don't know if anyone else even has this problem, or has noticed, but
This could be a very basic problem, but I sort of don't know SQL
It's not a problem but i don't know how to do this; let's say
I have a little Problem but don't know the Solution! I have wrote a
I know there are many threads that talk about this problem but I don't
I'm trying to look up this problem but I don't know what it's called.

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.