Skip to main content

simple mde duplicates

Javascript • Asked by TG

hi,
i followed your tutorial but still simple mde is been diplicated
am using rails 5

var simplemde;

simplemde = null;

$(document).on('ready turbolinks:before-visit', function() {
  if (simplemde != null) {
    simplemde.toTextArea();
    return simplemde = null;
  }
});

$(document).on('ready turbolinks:load', function() {
  return simplemde = new SimpleMDE({
    element: $("#textarea2")[0],
    showIcons: ["code", "table"]
  });
});

You've added "ready" to the event listeners which I did not have. Remove those so that it matches mine and your code will work.

You don't want to use the "ready" event for Turbolinks because that will cause your code to run twice on the first page load. Just use the Turbolinks events like I showed and you'll be fine.


i have removed thr ready but still the turbolinks duplicates the teaxtarea when i go forward and backward

var simplemde;

simplemde = null;

$(document).on('turbolinks:before-visit', function() {
  if (simplemde != null) {
    simplemde.toTextArea();
    return simplemde = null;
  }
});

$(document).on('turbolinks:load', function() {
  return simplemde = new SimpleMDE({
    element: $("#textarea2")[0],
    showIcons: ["code", "table"]
  });
});

I have the same issue

simplemde = null

$(document).on 'turbolinks:before-visit', ->
  if simplemde?
      simplemde.toTextArea()
        simplemde = null

$(document).on 'turbolinks:load', ->
  simplemde = new SimpleMDE(
      element: $('.simplemde')[0]
        spellChecker: false
    )

See my screenshot


For reference, here's what I'm using on GoRails. It's in Coffeescript and requires jQuery.

simplemde = null

cleanupSimpleMDE = ->
  # Clean up if already exists
  if simplemde?
    simplemde.toTextArea()
    simplemde = null

$(window).on 'popstate', cleanupSimpleMDE
$(document).on 'turbolinks:before-visit', cleanupSimpleMDE

$(document).on 'turbolinks:load', ->
  $mde = $(".simplemde")
  if $mde.length > 0
    simplemde = new SimpleMDE({
      element: $mde[0]
      toolbar: ["bold", "italic", "heading", "|", "code", "quote", "unordered-list", "ordered-list", "clean-block", "|", "link", "image", "|", "preview", "side-by-side", "fullscreen", "guide"],
      spellChecker: false
    })

@Chris After spending almost half a day on searching why the bug was still occuring even if I tried your code, I finally found the solution from this answer on StackOverflow, as I was also getting this error on firebug

jquery-ujs has already been loaded!

When I moved javascript_include_tag from the footer inside <head> the error and the bug was gone!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.