Reputation: 27679
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
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
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