May 24, 2011

mouseover() – using addClass() with this

Question by clarkk

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

Answer by KARASZI István

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');
  });

Answer by Starx

use

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!