check if Redactor initialised

609 views Asked by At

I am using Redactor WYSIWYG editor for my content edit. How can I check if redactor has been initialised on certain element ? I am currently checking if element has a class name redactor-editor, but I guess there must be better way?

$('.btn-edit-content').click(function(e){
    e.preventDefault();
    if(!$($(this).attr('href')).hasClass('redactor-editor'))
        $($(this).attr('href')).redactor({
            toolbarFixedTopOffset: 95,
            plugins: ['table']
        });
})
2

There are 2 answers

3
DFayet On

According to their api doc, you can do something like this

$('.btn-edit-content').click(function(e){
    e.preventDefault();

     if( $.data($($(this).attr('href'))[0], 'redactor') === undefined) {

         $($(this).attr('href')).redactor({
             toolbarFixedTopOffset: 95,
             plugins: ['table']
         });

     }
});

Look at their doc for more help

Hope it's gonna help you

0
trushkevich On

I haven't yet found a better way, so I'm checking whether Redactor is initialized or not this way (the snippet is in coffeescript):

$('.redactor').each ->
  $editor = $(this).siblings('.redactor-editor')
  $(this).redactor() unless $editor.length

context: I have several textareas having class .redactor on 1 page and at some point some of them lose Redactor's initialization. If I just call .redactor() for all textareas at once then previously initialized Redactors get duplicated.