Click here to Skip to main content
15,887,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have developed a small application for my own use to keep track of the hours I worked for customers.

There is a page there that includes a TinyMCE editor that I use to write down the tasks done.

I've added the SAVE plugin, which allows to submit the form the TinyMCE editor is embedded into.

And I'd like to be able to set the focus back to the TinyMCE editor when the page loads after the save event.

I've tried several approaches I've found on the Internet, but none worked for me.

I am using the latest version 6.7 (today).

The way I insert TinyMCE in my form is the following one:

JavaScript
<script language='javascript' type='text/javascript'>
			// Notice: The simple theme does not use all options 
            // some of them are limited to the advanced theme
			tinyMCE.init({
    		selector: '#notes',
				plugins: 'lists table save',
        toolbar: 'bullist numlist bold italic underline strikethrough subscript superscript indent outdent forecolor backcolor table save',
				width : '".$width."',
				height : '".$height."',
				branding: false,
				menubar:false,
				browser_spellcheck: true,
				resize: 'both',
       	});
		</script>


"notes" is the ID of the textarea that becomes replaced by the TinyMCE editor instance.

Do you know of the right syntax to do this?

Thank you all for your time and help.

What I have tried:

I've tried several methods but none of this worked for me:

JavaScript
<script>tinyMCE.get("#notes").focus();</script>
<script>tinyMCE.get("#notes").get().focus();</script>
<script>tinyMCE.get("#notes").getBody().focus();</script>
<script>tinyMCE.get({#notes}).focus();</script>
<script>tinyMCE.get(#notes_ifr).focus();</script>


Of course, I've called all those separately (to test one by one) and after the element creation.
Posted
Updated 13-Sep-23 7:38am
v2

1 solution

According to the documentation[^], you need to pass the auto_focus option in the init method:
JavaScript
tinyMCE.init({
    selector: '#notes',
    auto_focus: 'notes',
	plugins: 'lists table save',
    toolbar: 'bullist numlist bold italic underline strikethrough subscript superscript indent outdent forecolor backcolor table save',
	width : '".$width."',
	height : '".$height."',
	branding: false,
	menubar:false,
	browser_spellcheck: true,
	resize: 'both',

setup: function (editor) {
          editor.on('init', function () {
              editor.focus();
              editor.selection.select(editor.getBody(), true);
              editor.selection.collapse(false);
          });
      }});
 
Share this answer
 
v3
Comments
Joan M 14-Sep-23 9:35am    
This works! :jig:, but it sets the focus always, I can of course init the editor with the auto_focus clause only after updating it, but I would prefer being able to set the focus back at the same position it was just after submitting the form.

In any case, it's a major improvement compared on what I had before.

Thanks Richard!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900