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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:18:30+00:00 2026-06-05T15:18:30+00:00

I have the following JavaScript/jQuery code that starts the listener that highlights the DOM

  • 0

I have the following JavaScript/jQuery code that starts the listener that highlights the DOM elements.

I click a button and I start the listener event. ex: highlight : function()

When I click something in the webpage, I stop the listener.

Now, when I click the button again, I want to restart the listener event.

highlight : function()
    {
        if(isHighlighting){
            isHighlighting = false;
            $(document).unbind("mousemove", highlighter);
            return false;
        }

        $(document).bind("mousemove", highlighter);
        isHighlighting = true;
    },

I also have the code that catches the onclick event, and stops the DOM element highlighter

function highlighter(e) {
    var cur, overlay = $("#overlayhighlightclosetaffair"),
    no = [document.body, document.documentElement, document];
    if (e.target === cur) {
        return;
    }

    if (~no.indexOf(e.target)) {
        cur = null;
        overlay.hide();
        return;
    }

    var target = $(e.target),
    offset = target.offset(),
    width = target.outerWidth(),
    height = target.outerHeight();
    cur = e.target;
    overlay.css({
        top: offset.top,
        left: offset.left,
        width: width,
        height: height
    }).show();

    $(document).click(
        function() {
            if(isHighlighting){
                isHighlighting = false;
                $(document).unbind("mousemove", highlighter);
                console.log('click');
            }
    });
}
  • 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-05T15:18:32+00:00Added an answer on June 5, 2026 at 3:18 pm

    You don’t need to unbind the event, and can simplify your logic.

    This is not a fully functional example, but it should be a good starting point. I suggest structuring the code so that isHighlighting is not global.

    function highlightElement(e) {
        if(isHighlighting) {
            /* this is where you put the code to highlight e.target */
        }
    }
    
    /* call this one time */
    $(document).click(
        function(e) {
            /* this will be true if we clicked the button, false if we clicked anything else */
            isHighlighting = e.target === highlightButtonNode;
        }
    });
    
    /* also call this once */
    $(document).bind("mousemove", highlightElement);
    

    Here is an alternative solution using binding and unbinding. I do not recommend this approach, but this is a much better way the handle unbinding and rebinding. Forgetting to set isHighlight = false. Is a much less serious bug than forgetting to unbind the event. It would result in a memory leak and multiple calls to an event handler is more likely to have worse side effects.

    /* call this one time */
    $(document).click(
        function(e) {
            /* this will be true if we clicked the button, false if we clicked anything else */
            if(e.target === highlightButtonNode) {
                $(document).bind("mousemove", highlightElement);
            } else {
                $(document).unbind("mousemove", highlightElement);
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Javascript code (using jQuery): floatUpAndDown(); function floatUpAndDown() { $(#bird).animate({top: '+=30px'},
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following code in JavaScript and jQuery: $(<li />) .html('Some HTML') I
I have the following JavaScript code which I like to convert to jQuery but
I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =
I have the following code in a jQuery JavaScript document running on a page
I have the following js files in my code for EditInPlace... <?php echo $javascript->link('jquery.js');?>
I have the following Javascript and Html code that works fine in an MVC4
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
I have the following Javascript code that sorts divs on my page: $(function() {

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.