jquery – Add Class Dynamically

Question:

I have a carousel where the prev and next buttons are generated by the plugin.

The problem is even trying to add a class with the traditional method $(".owl-prev").addClass("theme-color"); the same does not apply because the div has not yet been generated.

Using the on function didn't work either:

$("#owl-main-banner").on("load", function(){
    $(".owl-prev").addClass("theme-color");
});

Answer:

You can use jQuery 's $.done method. When the plugin is generated then yes you call your addClass() .

Example:

var dfd = $.Deferred();
dfd.done( [ fn1, fn2 ], fn3, [ fn2, fn1 ] )

Where fn's are functions.

These will be executed in order of addition.

note:

If .done() doesn't work, try $.when() .

Scroll to Top