Setting values of TextArea via JavaScript

Setting values of TextArea via JavaScript

by Martin Holden -
Number of replies: 5

Hi everyone

I am just finishing of my first module for Moodle 1.9.x and am looking for guidance. I have a working solution to my issue, I am just not sure it is the best solution.

What I have is a new activity that when a user creates it (so code in mod_form.php) there is a field for a unique id for one of my companies courses.

I also have three textares as HTMLeditors on the form to contain variosu RichText descriptions etc

The user can fill in these textareas manually or as I want they can click a button, before they save the activity, that will call a web service (passing the id) which will return all the metadata.

Now my current working solution is to open a new window on the button click, this window makes the WebServices call and then via PHP the values are dumped into JavaScript function called by the page onload. The code is attached obviously no use without all my libs but you get the idea

Now the javascript call to set the textarea is:

window.opener.window.editor_<value1>.setHTML("What I want in the textarea");

Where value1 is the md5 or the textarea name.

So great this works BUT I am tying myself to the HTMLArea code which will be no more in Moodle 2.0, what I am after is ideas to make this work with ANY HTMLEDITOR wrapped textareas.

Any thoughts and guidance are greatly appreciated.

Thanks

Martin Holden

Average of ratings: -
In reply to Martin Holden

Re: Setting values of TextArea via JavaScript

by Sam Hemelryk -
Hi Martin,

You are certainly correct in realising that it ties you to HTMLArea and that the default editor in Moodle 2.0 is something different. Not only that but I believe the intention in Moodle 2.0 is to allow users to switch between editors.

Really what is needed, or should be provided is a Moodle JavaScript abstraction layer for the editor component that handles this sort thing for you by providing tidy functions that can determine the editor and call the appropriate method.
However I'm not aware of any such plans to put anything like this into place.

What I would certainly suggest at the moment is that you write you own intermediary JS method that tests for the editor and sets the content accordingly.
You could perhaps deliver this method with mod_form.php so that you could call:

window.opener.set_editor_content(id, content);

At least that way if you carry your module over to Moodle 2.0 you will only need to add in tests for the new editor(s).

Hope this helps
Sam
In reply to Sam Hemelryk

Re: Setting values of TextArea via JavaScript

by Martin Holden -

Thanks Sam

Usual issue of been to close to things, makes perfect sense to abstract the setting function.

Martin

In reply to Martin Holden

Re: Setting values of TextArea via JavaScript

by Frank Ralf -
Hi Martin,

I don't know whether this is too much, but Moodle comes with YUI support so you could resort to YUI's AJAX capabilities, e.g.

Rich Text Editor: Editor Data Post with Connection Manager
"This example demonstrates how to use Connection Manager to post data to the server, filter it and return it to the Editor."

See Javascript FAQ for further hints on incorporating JavaScript into Moodle.

hth
Frank
In reply to Frank Ralf

Re: Setting values of TextArea via JavaScript

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi Frank,
Just clicked on the Javascipt FAQ link you mention in your post, and it takes me to

Javascipt FAQ

From MoodleDocs

This page does not exist yet. You are welcome to create it.

In reply to Joseph Rézeau

Re: Setting values of TextArea via JavaScript

by Frank Ralf -
Thanks for the hint, just a typo (missing "r"). I have just created that page with a redirect in case it happens again wink

Frank