Forum Discussion

PerBonomi's avatar
11 years ago

Edit the body of a PM (tinymce) with jquery?

Hi folks.

I've just made a component that will pre-fill a new PM to send to a user when you move their post.

 

Only issue is, I can't seem to edit the body of the PM.

 

Can anyone tell me how to edit that tinymce field with jquery?

 

Right now I'm putting the text in the subject line (for a cut/paste), but that's not ideal :/

 

SendPMOnMove.JPG

8 Replies

  • The editor for the message body is in an iframe, so you need to get a reference to the iframe first, then change the content.

     

    On my test community, this code sets the content of the message to "Hello World!":

    LITHIUM.jQuery('#tinyMceEditor_ifr').contents().find('#tinymce').html('<p>Hello World!</p>')

     

    Note that Lithium may not always use the iframe (e.g. on mobile devices or older browsers).

  • PerBonomi's avatar
    PerBonomi
    Boss
    11 years ago

    I wonder what tinymce version you're on? Becuase we ar eon the latest one, that Lithium updated, and nothing happens when I run that script.

  • nathan's avatar
    nathan
    Executive
    11 years ago

    Just checked on the live Spotify community, and the code works for me. You need to make sure the code runs after the document has loaded.

     

    Stumbled across another way of setting the content:

    tinyMCE.activeEditor.setContent("Hello world!")

    Found this in the Lithium core JavaScript code. It works when I run it from the console on the private message page.

  • PerBonomi's avatar
    PerBonomi
    Boss
    11 years ago

    In the console it runs just fine, but if I put the script in the header, nor the footer.

     

    The tinymce editor sort of blips when it's done loading itself. I think it' firing some of its own code near the end, which I think is interrupting other code. This doesn't work:

     

    $(document).ready(function () {
    LITHIUM.jQuery('#tinyMceEditor_ifr').contents().find('#tinymce').html('<p>Hello World!</p>')
      });

     

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    Hi PerBonomi 

     

    I haven't tried myself - but I think TinyMCE has also a "init" event that you need to observe before executing extra logic, to ensure your custom script runs when the editor is fully initalized.

     

    Thanks,

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    Just a word of caution, the documentation linked above is for TinyMCE 3.x , but we recently upgraded to 4.x so it may differ between communities etc ...