HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Which mouseover handler is better?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
whichhandlermouseoverbetter

Problem

I'm currently learning some jQuery and I am stuck on something. On a small exercise I did, I was asked to perform an action when you mouseover a div class. I gave a different solution than the one suggested and I was wondering which one is written best and why.

HTML code


   
   
   


Here is my solution:

$(document).on('mouseover', '.success', function() {
    $(this).removeClass('success').addClass('delete');
});


And here is his solution:

$(document).on('mouseover', '.deletecell', function() {
    $('.success', this).addClass('delete').removeClass('success');
});


PS: Does it matter if addClass is first and removeClass is second?

Solution

Yours is simpler and therefore better. However, I would simplify it further as:

$('.success').on('mouseover', function() {
    $(this).removeClass('success').addClass('delete');
});


Is shouldn't matter much whether you call .addClass() first or .removeClass() first.

Code Snippets

$('.success').on('mouseover', function() {
    $(this).removeClass('success').addClass('delete');
});

Context

StackExchange Code Review Q#53966, answer score: 3

Revisions (0)

No revisions yet.