clarkk
clarkk

Reputation: 27679

mouseover() - using addClass() with this

how can I add a class when mouse is over an element?

var row = $('<tr></tr>')
    .append('<td class="list_row">'+js2txt(string)+'</td>')
    .mouseover(function(){
        this.addClass('list_row_mover');
    });

js error:

this.addClass is not a function

Upvotes: 0

Views: 248

Answers (4)

JohnP
JohnP

Reputation: 50019

You need to use $(this), which is the jquery object.

Upvotes: 0

user657496
user657496

Reputation:

this returns to the DOM object which has no function addClass. Instead, user $(this) to point to the jQuery object, which does has the function addClass.

var row = $('<tr></tr>')
.append('<td class="list_row">'+js2txt(string)+'</td>')
.mouseover(function(){
    $(this).addClass('list_row_mover');
});

Upvotes: 1

Starx
Starx

Reputation: 78981

use

$(this).addClass("list_row_mover");

Upvotes: 0

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31467

In your function the scope (this) is the HTML element, not the jQuery object.

Here is a possible fix:

var row = $('<tr></tr>')
  .append('<td class="list_row">'+js2txt(string)+'</td>')
  .mouseover(function(){
    $(this).addClass('list_row_mover');
  });

Upvotes: 4

Related Questions