How to get a text insider the HTML editor

How to get a text insider the HTML editor

by hon wai lau -
Number of replies: 4

Hello, I want to know a way to a way to get the text insider the HTML editor in the Moodle 2.0+. I have searched a little bit in this forum but cannot find an answer.

In the Moodle 1.9, I can use the following code like

document.getElementsByName('questiontext')[0].value

to get the text inside the HTML editor. Surely, I have to switch on and off between the text mode for it to work properly. However, it does not work for the Moodle 2.0 and the content return by the above script is something like a class of tinymce. So, anyone know a way to get the text. Any help would be appreciated.

Average of ratings: -
In reply to hon wai lau

Re: How to get a text insider the HTML editor

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Why do you need to do that in JavaScript code? Intriguing.

Anyway, Moodle 2.x uses TinyMCE, so I suppose you need to look at it API.

In reply to Tim Hunt

Re: How to get a text insider the HTML editor

by hon wai lau -

Well, I am porting to the formulas question type to Moodle 2.0 and which has a simple functionality to dynamically display the question with instantiated variables. It just get the text and replace all variables inside the text for a quick reference.

I think someone can give me a quick answer and I am also not sure how to get the tinymce object in Moodle.

In reply to hon wai lau

Re: How to get a text insider the HTML editor

by Mathias Tausen -

When using tinyMCE, use the get() and getContents() methods to get the contents.

tinyMCE.get('editor1').getContent();

Replace editor1 with the id of the editor you're trying to fetch the contents of.

In reply to Mathias Tausen

Re: How to get a text insider the HTML editor

by hon wai lau -

Thanks, it works. I find that the new javascript cache is quite unfriendly for the firebug and it is difficult to debug. Also, sometimes the editor does not initialize and it causes an exception, so I use the following instead:

        try {
            var globaltext = tinyMCE.get('id_questiontext').getContent();
        }
        catch(e) {
            var globaltext = document.getElementById('id_questiontext').value;
        }