Search code examples
javascriptjqueryonclickckeditorckeditor4.x

CKEDitor 4 sefocus on clicking label


I have a CKEditor 4 instance:

  <label for="message_text_1_0">Kurzfassung des Beitrags</label>
<div style="float:left">


<textarea id="message_text_1_0" name="message_text_1_0">
</textarea>
</div>
<script>CKEDITOR.replace('message_text_1_0', {"customConfig": "/my/path/file.js", "Width": "600px", "External": 0, "Height": "100px", "DefaultLanguage": "de", "language": "en"});<script>

Everything works fine so far, but I want a click on the label, to pass the focus to the editor.

Unfortunately neither

$(document).on('click', 'label', 
    function () {
        let target = $(this).attr('for'); 
        if (CKEDITOR.instances[target]){ 
            CKEDITOR.focusManager(CKEDITOR.instances[target]).focus(); 
            return false; 
        }
    }
)

nor

$(document).on('click', 'label', 
    function () {
        let target = $(this).attr('for'); 
        if (CKEDITOR.instances[target]) { 
            CKEDITOR.instances[target].focus();
            return false; 
        }
    }
)

work.

Putting these into setTimeout hasn't helped either. (JQuery is loaded and used)

Any help would be appreciated!


Solution

  • $(document).on('click', 'label', 
        function (ev) {
            let target = $(this).attr('for'); 
            if (CKEDITOR.instances[target]) {
                CKEDITOR.instances[target].focus(); 
                ev.preventDefault();
                return false; 
            }
        }
    )
    

    was the solution.