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

  • Home
  • SEARCH
  • 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 7949721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:47+00:00 2026-06-04T02:03:47+00:00

Im having some problems with .on() and how to use it instead of .bind()

  • 0

Im having some problems with .on() and how to use it instead of .bind() in this situation.
What im trying to do here is i click a link and that is supose to bind another click event, but instead it triggers that event right away. I looked in the documentation/jquery.js file and this is how im suppose to do it.

Demo: http://jsfiddle.net/bNaFV/

$('#click_me').on('click', function(){    
    $('#show_me').addClass('remain');

    //this is only suppose to bind that next time i click anywhere on the document it should hide
    // not right away
    $(document).on('click', 'html', function(){
      $('#show_me').hide();        
    });
});

$("#click_me").hover(
  function () {
    $('#show_me').show();
  }, 
  function () {
      if ($('#show_me').hasClass('remain')){
           return;   
      } else {
          $('#show_me').hide();   
      }
  }
);



<a href="#" id="click_me">click me</a><br /><br />

<div id="show_me"></div>​
  • 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-04T02:03:48+00:00Added an answer on June 4, 2026 at 2:03 am

    You need to stop the propagation of the event:

    $('#click_me').on('click', function(e){    
        e.stopPropagation(); //Stop the event from bubbling further
        $('#show_me').addClass('remain');
        $(document).on('click', 'html', function(){
            $('#show_me').hide();        
        });
    });
    

    This is because the event has been captured at the #click_me element. You then bind an event handler for that same event type somewhere higher up the DOM tree. The event then continues bubbling up the tree and reaches the document, where it triggers the new event handler.

    Here’s a working example.

    Update (see comments)

    As noted by @zerkms in the comments, I think you probably only want to bind the event handler to document once. You could use the one method to do so, which unbinds the event handler after it’s been executed once:

    $(document).one('click', 'html', function(){
        $('#show_me').hide();        
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems with getting the .getruntime.exec() to work properly. Here is the
I'm having some problems implementing an algorithm to read a foreign process' memory. Here
I've been trying to use this plugin -> http://code.drewwilson.com/entry/autosuggest-jquery-plugin (broken link, see github ),
Currently having some problems- now = datetime.datetime.now() month = now.strftime(%B) site = wikipedia.getSite('en', 'wikiquote')
Im having some problems getting the Sticky Footer to work on my site. If
Im having some problems with a simple toggle in Safari. A really annoying flickering
I'm having some problems getting ncurses' getch() to block. Default operation seems to be
The background: I'm having some problems with Thoughtbot's Factory Girl gem, with is used
I'm having some problems with eVB. How can I put a menu bar in
I am having some problems turning the SQL below into a Zend Db query.

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.