Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE and Bootstrap Modals -- Works only one time

I'm using Bootstrap and tinymce-rails so that I can have a nice text editor for some of my text areas. However, I'm having a modal render a form that contains a textarea and the "tinymce" class, but this modal only actually shows the TinyMCE text editor one time. Once the modal is closed and re-opened, it only looks like a regular text field.

Here's the form that's being rendered:

<%= form_for @paragraph_section, html: {class: "form-horizontal"} do |f| %>
<div class="form-group">
  <%= f.label :paragraph, nil, class: "col-sm-3 control-label" %>
  <div class="col-sm-6">
    <%= f.text_area :paragraph, placeholder: "(e.g. Hello World)", class: "form-control tinymce" %>
  </div>
</div>
<div class="modal-footer">
  <%= f.submit "Add paragraph", class: "btn btn-xs btn-primary" %>
</div>
<% end %>

Now when I click on the "New paragraph" link, which is sends a remote call to new.js.erb, here's this modal pops up and the tinymce text editor actually works. But again, once I close this and re-open the mdoal with the "new paragraph" link again, the tinymce text editor doesn't work.

Here's what the new.js.erb file looks like:

$('#modalOne').modal({show: true});
$('#modal_content').html("<%= j render 'form' %>");
$('#modal_header').html("New Paragraph");

tinymce.init({
    selector: "textarea",
    width:      '100%',
    height:     270,
    plugins:    [ "anchor link" ],
    statusbar:  false,
    menubar:    false,
    toolbar:    "link anchor | alignleft aligncenter alignright alignjustify",
    rel_list:   [ { title: 'Lightbox', value: 'lightbox' } ]
});

Any idea how I can have the tinymce text editor working, despite me closing and re-opening the same modal?

like image 286
LewlSauce Avatar asked Nov 16 '14 19:11

LewlSauce


People also ask

How do Bootstrap modals work?

Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead. Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

Do Bootstrap modals work on mobile?

Bootstrap modals don't work correctly on Android and iOS. The issue tracker acknowledges the problem but does not offer a working solution: Modals in 2.0 are broken on mobile. The screen darkens but the modal itself is not visible in the viewport.

When should I use Bootstrap modal?

Bootstrap Modals offer a lightweight, multi-purpose JavaScript popup that's customizable and responsive. They can be used to display alert popups, videos, and images in a website.


2 Answers

I found that using the below when the modal is closed works perfectly fine:

    $('#modalOne').on('hide.bs.modal', function () {
    tinyMCE.editors=[];
    });
like image 75
LewlSauce Avatar answered Oct 24 '22 21:10

LewlSauce


The solution didn't work for me with TinyMCE 5 and Bootstrap 4. In the end I followed Abdur's link and had to use the remove method.

 $('#modalOne').on('hide.bs.modal', function () {
    // scope the selector to the modal so you remove any editor on the page underneath.
    tinymce.remove('#modalOne textarea');
 });
like image 27
rip747 Avatar answered Oct 24 '22 22:10

rip747