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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:25+00:00 2026-05-28T22:09:25+00:00

I would like to unbind an event on a div that contains a button

  • 0

I would like to unbind an event on a div that contains a button when this button is clicked
I am using this piece of code but it doesn’t seem to work, the event keeps being attached to the button container:

$('body').on('click', '.button', function(){
        $('body').off('mouseleave',$(this).closest('.button-container'));
});

what am I doing wrong?

fiddle link: http://jsfiddle.net/y3a6N/3/

Thanks

  • 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-05-28T22:09:25+00:00Added an answer on May 28, 2026 at 10:09 pm

    You can’t use .on() and .off() that way. Your .on() code is a delegated handler:

    $('body').on('hover', '.button-container', function(){
       $(this).append('<p>New text</p>');
    });
    

    It’s an event handler attached to the body object and looking for hover events from any element with class of “button-container”.

    You cannot then just do .off() on one particular element like you are trying to do with this:

    $('body').off('hover',$(this).closest('.button-container'));
    

    That doesn’t match the .on() signature so there’s no matching event handler to remove. With delegated event handler (like you’re using here with .on()), it’s either all of nothing. You either remove the entire event handler or not. You can’t tell it to just stop delegating on behalf of only some objects.

    If your objects are not dynamic and thus you don’t need delegated event handling, you can do this to remove the specific event handler you want to remove:

    function addText() {
       $(this).append('<p>New text</p>');
    }
    
    $('.button-container').on('hover', addText);
    
    $('.button').on('click', function(){
       $(this).closest('.button-container').off('hover', addText);
    });
    

    Working jsFiddle here: http://jsfiddle.net/jfriend00/R2eQx/.


    Another approach that might be simpler is to just remove the class from the object like this:

    <div class="button-container active">
        <div class="button" style="border: 1px solid #cc0000">Roll over me to make text appear / Click me to stop making text appear (doesn't work)</div>
    </div>
    
    $('body').on('hover', '.active', function(){
       $(this).append('<p>New text</p>');
    });
    
    $('body').on('click', '.button', function(){
       $(this).closest(".button-container").removeClass("active");
    });
    

    And a working jsFiddle here: http://jsfiddle.net/jfriend00/byFpx/.

    This leaves the event handler in place, but just removes the triggering class from the button-container so it won’t match the event handler any more. This makes it easier to make it trigger again by just adding the class back again.

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

Sidebar

Related Questions

I've delcared a click callback on a div that I would like ignored if
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,
I'm not sure how to describe this, but I'll try with some basic code:
Would like to read RSS in VIM, but I haven't found a plugin out
would like to print a random number between 0 and 10, but generate seems

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.