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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:19:44+00:00 2026-06-04T04:19:44+00:00

Im trying to bind/live a click event when an other is done. So I’m

  • 0

Im trying to bind/live a click event when an other is done. So I’m using .on() and .off(). The way is suppose to act is that when

  1. i click the a.nav-link a div shows up,
  2. and when it does i pin a click event to the document,
  3. it binds a click event that when ever i click anywhere in the html it close the div and off() the document behavior

now it works fine for one round but next time i click the a.nav-link it performs all the tasks at once, so what I’m i doing crazy here?

Demo: http://jsfiddle.net/xHNQ3/1/

<div id="container">
    <header>
        <div class="nav">
            <a href="#" id="nav-link">click me</a>
        </div>
        <div class="sub-nav"></div>
    </header>
</div>​

    $('#nav-link').on('click', bindOnClick);


     function bindOnClick() {
         $('.nav').addClass('isVisible');
         $('.sub-nav').show(function(){
             $(document).on('click', 'html', function(){
                 alert('done');
                 $('.sub-nav').hide();

                 $(document).off('click', 'html', bindOnClick);
             });
         });
     }

EDIT: what I’m trying to do here is basically that whenever i click outside the two divs the one that shows up .sub-nav should hide.

  • 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-04T04:19:45+00:00Added an answer on June 4, 2026 at 4:19 am

    This line is your problem, bindonclick is associated with the link, not with the document click.

    $(document).off('click', 'html', bindOnClick);
    

    http://jsfiddle.net/xHNQ3/4/

    $(document).off('click', 'html');
    

    If you want to prevent clicking of link from causing a double event fire then turn it off/on too.

    http://jsfiddle.net/xHNQ3/9/

    $('#nav-link').on('click', bindOnClick);
    
    
    function bindOnClick(e) {
        $('#nav-link').off('click', bindOnClick);
        $('.nav').addClass('isVisible');
        $('.sub-nav').show(function() {
            $(document).on('click', 'html', function() {
                alert('done');
                $('.sub-nav').hide();
    
                $(document).off('click', 'html');
                $('#nav-link').on('click', bindOnClick);
            });
        });
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind a click event handler to some elements that are being
I am trying to bind the click event to an element dynamically generated using
I'm trying to bind three events to one selector using the live() method, but
I have an unordered list (UL) that I'm trying to bind a mouseover/mouseenter event
I'm trying to bind parameters in what I think is the correct way, but
I'm trying to bind a XAML ComboBox so that its list items are the
I've done some experimenting, but can't seem to successfully bind one event handler to
I'm trying to bind Fancy box links so that when the new links are
I'm trying to highlight the text that matches the query in a live search
I am trying to bind click handlers to incoming ajaxed content. I used to

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.