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