0

I am using tinyMCE editor. I need to set focus on the tinyMCE text area if jQuery validation fails. I have tried number of methods but all failed to set focus. I have tried:

    tinyMCE.activeEditor.focus;
    
I have tries this also :
    tinyMCE.activeEditor.execCommand('mceFocus', false, 'textarea_id'); 
    
and also
    $("#lCmsContent").tinyMCE().focus();
    

Actually i am using jQuery validation, so i need to set focus if the validation fails in case of text editor. For this my code is as follows:

        $('.form_validation_cmspage').validate({
                onkeyup: false,
                errorClass: 'error',
                validClass: 'valid',
                highlight: function(element) {
                    $(element).closest('div').addClass("f_error");
                },
                unhighlight: function(element) {
                    $(element).closest('div').removeClass("f_error");
                },
                errorPlacement: function(error, element) {
                    $(element).closest('div').append(error);
                },
                rules: {
                    'vCmsTitle'     : { required        : true},
                    'lCmsContent'       : { required        : true},
                    'status'            : { required        : true}
                }, messages: {
                    'vTitle'        : { required:   "This field is required!"},
                    'lCmsContent'       : { required        : "Description is required!"},
                    'status'            : { required:  'Status field is requerid!' }
                },ignore: ':hidden:not(.chzn_a)',
                invalidHandler: function(form, validator) {
                    var errors = validator.numberOfInvalids();
                    if (errors) {
                        if(validator.errorList[0].message == 'Description is required!')
                        {
                            tinyMCE.activeEditor.execCommand('mceFocus', false, 'lCmsContent');
                        }
                    }
                }
            })
    

For text editor:

<textarea id="lCmsContent" name="lCmsContent"></textarea>

Thanks in advance!!

7
  • why you left id param blank tinymce.execCommand('mceFocus',false,'id_of_textarea'); ? Jul 11, 2014 at 7:46
  • @MohitArora Yes i forgot to mention it here, i added the above. but this did not work Jul 11, 2014 at 8:30
  • Is there any other way out Jul 11, 2014 at 8:37
  • did you try any from these stackoverflow.com/questions/2212552/… Jul 11, 2014 at 9:15
  • 1
    yeaaa :) Thanks @MohitArora, tinyMCE.get('Description').getBody().focus(); this one worked for me Jul 11, 2014 at 9:21

2 Answers 2

0

Try this

$("#your_texbox_id").focus();

UPDATE:

I assume that "activeEditor" is a variable of tinymce initialization. This should work:

activeEditor.execCommand('mceFocus',false,'id_of_textarea');
1
  • I have tried this also but this is not helping. I am also checking the firenug, there is no issue in the console also :( Jul 11, 2014 at 8:53
0

In my case just set auto_focus: '#textarea_id' worked.

tinymce.init({
    selector: '#textarea_id',
    auto_focus: '#textarea_id'
});

In TinyMce Documentation also - https://www.tinymce.com/docs/configure/integration-and-setup/#auto_focus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.