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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:45:05+00:00 2026-06-01T03:45:05+00:00

I’m adding a class on hover to various links, works great. However, when you

  • 0

I’m adding a class on hover to various links, works great. However, when you click on the link, sometimes the new added class stays or the JS becomes unstable. I’m guessing I need to add a remove class onclick?

$("a").hover(function () {
    switch ($(this).attr("href")) {
        case "somelink1.html":
            parent.top.$(".start-box").toggleClass("tutorialHover");
            break;
            case "somelink2.html":
            parent.top.$(".end-box").toggleClass("tutorialHover");
            break;
            // etc..
  • 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-01T03:45:06+00:00Added an answer on June 1, 2026 at 3:45 am

    I don’t see the full code here (truncated by an etc) but hover expects two functions. It’s just an encapsulation of mouseenter and mouseleave in one. You just need to addClass in the enter part and removeClass in the exit part. The click shouldn’t have anything to do with it unless you do indeed expect something different if you’re still hovering and yet have also clicked.

    Toggling class is better for single events like click. Hover always has at least the potential to always trigger the in and also the out (unless you just never move your mouse!) so be explicit about when the class should be added and when it should be removed.

    http://jsfiddle.net/As2dF/

    D’oh, just realized you can set the same event for both in and out… I’ll leave my answer anyhow since I do feel it’s better to be explicit: two functions—one in and one out—but I’m wrong that you NEED to supply two.

    [update]

    Experimenting with it as one function doing double-duty (ie. toggleClass in the one function instead of addClass and removeClass and unsurprisingly it’s impossible to reproduce the click problem. This leads me to believe that it’s either:

    1. Noticed that you’re not preventing default behaviour on the links. A plausible guess is that you’re hovering (adding class), clicking (refreshing page) and your mouse is still in position over the NON-CLASSED element; when you move out, class is then toggled. If it’s this problem or even something related, this is how being explicity about addClass and removeClass (instead of toggleClass) ensures it’s doing what you want it to do. That said, if this IS the problem, you can update the beginning of your function to look like this so that the href doesn’t get triggered.

      $("a").hover(function (e) {
        e.preventDefault();
        // etc
      
    2. selector error (you are adding and removing from two different “levels” that happen to overlap, causing classes to get super wonky

    3. related to #1, an event bubbling error. Although you may not ultimately want to stop propagation altogether, see if this experiment works– for your trigger function, pass the event and stop propagation on it:

      $("a").hover(function (e) {
        e.stopImmediatePropagation();
        // etc
      

    This may cause other unwanted side-effects (maybe other events aren’t being triggered that need to be!) but at least it will help isolate bubbling issues.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.