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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:31:09+00:00 2026-06-13T11:31:09+00:00

I am using mouseenter and mouseleave events on some elements to change their appearance.

  • 0

I am using mouseenter and mouseleave events on some elements to change their appearance. I am able to do so using either of the following two strategies:

  1. $(this).css('someProperty','someValue') to add and then $(this).removeAttr('style') to remove
  2. $(this).addClass('someClass') to add and then $(this).removeClass('someClass') to remove

What is the best way to do so?

  • 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-13T11:31:10+00:00Added an answer on June 13, 2026 at 11:31 am

    Definitely option 2. Styles should be defined in the stylesheet.

    There’s also toggleClass, which removes the class if it’s there, but adds it if it’s missing.


    Note: toggleClass also lets you pass in a Boolean as the second argument, telling it whether to add or remove it (regardless of whether it currently has that class applied), so:

    $(this).toggleClass('active', true);
    

    is exactly equivalent to:

    $(this).addClass('active');
    

    This is very handy when you have a Boolean in your code already. So instead of this:

    if (isUserActive()) {
        $(this).addClass('active');
    } else {
        $(this).addClass('active');
    }
    

    You could just do this:

    $(this).toggleClass('active', isUserActive());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
I'm trying to create an .on() method using events-map. I want to change this:
I'm using mouseenter and mouseleave events in jquery on an unordered list's <li> to
I'm using http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.js and I'm not sure how to use the mouseenter and mouseleave
I have 3 banners in which I want to have some mouseenter and mouseleave
By default flowplayer tooltip opens on mouseenter,mouseleave events. I want to override it, so
http://jsfiddle.net/nicktheandroid/ppNaX/ This is somewhat theoretical. The menu shows/hides on mouseenter/mouseleave, with a toggle button
I'm using mouseenter/mouseleave on a div for fading it in and out. The problem
I'm having some difficulty wrapping my head around this one. I have two divs,
Have a look at this jsFiddle The mouseenter / mouseleave don't appear to be

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.