Summernote: set focus after initialization
Asked Answered
V

7

12

Summernote gives you the option of giving focus to the editor when you create it, passing this options:

$('#summernote').summernote({
     focus: true
});

But after initialization, it seems you can't give focus to the textarea by clicking a button or similar. I've tried several ways, without success.

Anyone did that?

Vicious answered 23/10, 2014 at 22:41 Comment(0)
B
16
$('.summernote').summernote('focus')

ps. i want to find an angular way out .. and it works

Basidiomycete answered 24/8, 2015 at 10:33 Comment(1)
doesn't look like an answer. Consider deleting 'cause it may ran into downvote-fall.Risteau
V
4

After coming back to this problem and trying a lot of solutions, I came to one that works:

$('.note-editable').trigger('focus');

Triggering the event through jQuery works, but using the focus() function doesn't.

Vicious answered 3/7, 2015 at 22:20 Comment(3)
Did you find out why?Mcbrayer
I have no idea. jQuery's doc says .focus() is just a shortcut for this syntax, so I don't really know if there's a difference.Vicious
What if someone want to do reverse, as in summernote 0.8.8 at the moment is getting focus on form init. I don't want this behavior.Kabob
A
1
$(document).ready(function () {
                        $.fn.extend({
                            placeCursorAtEnd: function () {
                                // Places the cursor at the end of a contenteditable container (should also work for textarea / input)
                                if (this.length === 0) {
                                    throw new Error("Cannot manipulate an element if there is no element!");
                                }
                                var el = this[0];
                                var range = document.createRange();
                                var sel = window.getSelection();
                                var childLength = el.childNodes.length;
                                if (childLength > 0) {
                                    var lastNode = el.childNodes[childLength - 1];
                                    var lastNodeChildren = lastNode.childNodes.length;
                                    range.setStart(lastNode, lastNodeChildren);
                                    range.collapse(true);
                                    sel.removeAllRanges();
                                    sel.addRange(range);
                                }
                                return this;
                            }
                        });
                    });

then:

$('.note-editable').click(function(){
                                  //$('#summernote').summernote('focus');
                                  $(this).placeCursorAtEnd();
                                });

①focus on click or tap ②focus at the end of content

it works on mobile device too

Acetyl answered 20/3, 2018 at 7:5 Comment(0)
P
0

please refer codepen for this problem

/* Summernote Validation */

$(function () {
    var summernoteForm = $('.form-validate-summernote');
    var summernoteElement = $('.summernote');

    var summernoteValidator = summernoteForm.validate({
        errorElement: "div",
        errorClass: 'is-invalid',
        validClass: 'is-valid',
        ignore: ':hidden:not(.summernote),.note-editable.card-block',
        errorPlacement: function (error, element) {
            // Add the `help-block` class to the error element
            error.addClass("invalid-feedback");
            console.log(element);
            if (element.prop("type") === "checkbox") {
                error.insertAfter(element.siblings("label"));
            } else if (element.hasClass("summernote")) {
                error.insertAfter(element.siblings(".note-editor"));
            } else {
                error.insertAfter(element);
            }
        }
    });

    summernoteElement.summernote({
        height: 300,
        callbacks: {
            onChange: function (contents, $editable) {
                // Note that at this point, the value of the `textarea` is not the same as the one
                // you entered into the summernote editor, so you have to set it yourself to make
                // the validation consistent and in sync with the value.
                summernoteElement.val(summernoteElement.summernote('isEmpty') ? "" : contents);

                // You should re-validate your element after change, because the plugin will have
                // no way to know that the value of your `textarea` has been changed if the change
                // was done programmatically.
                summernoteValidator.element(summernoteElement);
            }
        }
    });

});
Phonetist answered 26/10, 2018 at 7:49 Comment(0)
B
0

This works for Summernote v0.8.18:

$(`div.note-editing-area div.note-editable`).focus();
Briggs answered 20/9, 2022 at 20:36 Comment(0)
S
-2

You can set the focus by putting the focus on the editable div used by Summernote:

document.querySelectorAll(".note-editable").focus();

or using JQuery

$('#summernote').find('.note-editable').focus();
Sue answered 9/12, 2014 at 20:41 Comment(2)
didn't work. at least on chrome. I can find the div but the focus don't work :(Vicious
maybe try to set the focus with a little timeout: setTimeout(function() { $('#summernote').find('.note-editable').focus(); }, 200);?Sue
P
-3
<div class="col-lg-12 col-md-6 col-xs-12">
    <div class="form-group">
        <label>  Skills</label>
        <span ng-messages="EditForm.Skills.$error" ng-if="EditForm.Skills.$dirty || isEditFormSubmitted ">
            <span class="text-danger" ng-message="required" ng-bind="error msg"></span>
        </span>
        <text-angular id="Skills" name="Skills" ng-model="EditVacancyModel.Skills" ta-toolbar="[['bold','underline','italics','ul','ol']]"ng-required="true"></text-angular>
    </div>
</div>
Pericles answered 16/2, 2020 at 8:13 Comment(1)
OP specified jquery. Also no explanation. -1Nina

© 2022 - 2024 — McMap. All rights reserved.