user3284463 user3284463 - 6 months ago 10
Javascript Question

addClass() not adding class

I'm trying to add a class to a element but it doesn't seem to be adding the class.

This is what my code looks like:

var wrapper='<div class="float-notification"></div>';
var message="<span>" + message + "</span>";
if(error==true){
var close_button = '<span class="float-notification-close" style="float:right;cursor:pointer;">X</span>';
var content = $(wrapper).append(message + close_button);
$(this).append(content);
//give wrapper a error class
$(wrapper).addClass('error');
}else{.....


I've also tried this with
$(wrapper).append("Foo Bar")
and it works as expected, So why won't
addClass()
work?

Answer

U need to append it to body

var wrapper='<div class="float-notification"></div>';
$(wrapper).addClass('error').appendTo('body');

It works!