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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:10:48+00:00 2026-05-29T20:10:48+00:00

I want to prevent two events from being watched for an element (mouseenter and

  • 0

I want to prevent two events from being watched for an element (mouseenter and mouseleave to be specific) if that certain element doesn’t have a css class defined.

The flow of this system is

  1. On document ready, I add the class .selected to the #menu_top_option_1 element

    $("#menu_top_option_1").addClass("selected");
    
  2. There is a sensor for those two events

    $("#menu_top_option_1").not(".selected").on({
        mouseenter: function(){
            $(this).addClass("highlighted");
        },
        mouseleave: function(){
            $(this).removeClass("highlighted");
        }
    });
    
  3. When there is a click event I add the class .selected to the #menu_top_option_1 element.

    $("#menu_top_option_1").click(function () {
        $("#menu_top_arrow_img").animate({left: position_menu_top_option_1+'px'}, 500);
        $(this).removeClass("highlighted");
        $("#option_wrapper div.option").removeClass("selected");
        $(this).addClass("selected");
    });
    

The problem: this does not work as expected. The elements still have the same hover events as they did before being assigned the .selected even after adding .selected to them. Except for the #menu_top_option_1 element, because I suspect it does have that class added at document ready.

Any suggestions?

  • 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-29T20:10:50+00:00Added an answer on May 29, 2026 at 8:10 pm

    You should bind the event handler to all of the elements and then inside that event handler, check to see if the current element being clicked has the selected class:

    $(function () {
        //bind a click event handler to all of the menu items that:
        //1. removes the `selected` class from the element that currently has it
        //2. applies that class to the element clicked
        //3. removes the `highlighted` class from the current element since it will have it, due to the fact that you have to hover over an element to click it
        $('#option_wrapper').children().on('click', function () {
            $(this).siblings('.selected').removeClass('selected').end().removeClass('highlighted').addClass('selected');
    
        //bind a mouseenter event handler to the menu items that updates the currently moused-over element only if it doesn't have the `selected` class
        }).on('mouseenter', function () {
    
            //notice the `!` here, so we are checking for the non-existence of the class in question
            if (!$(this).hasClass('selected')) {
                $(this).addClass('highlighted');
            }
    
        //bind a mouseleave event handler to the menu items that removes the `highlighted` class
        }).on('mouseleave', function () {
            $(this).removeClass('highlighted');
        });
    });
    ​
    

    Here is a demo: http://jsfiddle.net/g8W4z/

    Some documentation:

    • .hasClass(): http://api.jquery.com/hasclass
    • .siblings(): http://api.jquery.com/siblings
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I prevent JavaScript from handling two events at once? I have an
I have two screens on my app that I want to present to users,
I want to prevent my asp.net / c# 2008 web pages from being cached
What solutions do I have if I want to prevent the UI from freezing
When I want to prevent other event handlers from executing after a certain event
How to prevent a race condition in MySQL database when two connections want to
I want to prevent some parts of my javascript, written with jQuery if that
I want to prevent users from commenting on deleted threads. I've set a variable
I want to prevent iframe elements from triggering the OnDocumentComplete event every time. For
I have two wxListCtrl and want to process the Ctrl + Enter keyboard event

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.