Textarea value change when summernote div is changed

前端 未结 6 1628
独厮守ぢ
独厮守ぢ 2021-02-05 09:37

I have setup a div for the summernote to alter text pulled from a database.

6条回答
  •  青春惊慌失措
    2021-02-05 10:23

    Inspired by @user3367639, this is a more generic way to do it

    $('.summernote').each(function () {
        var $textArea = $(this);
    
        $textArea.summernote({
            onKeyup: function (e) {
                $textArea.val($(this).code());
                $textArea.change(); //To update any action binded on the control
            }
        });
    });
    

    And with this method :

    String.prototype.strip = function()
    {
       var tmpDiv = document.createElement("div");
       tmpDiv.innerHTML = this;
       return tmpDiv.textContent || tmpDiv.innerText || "";
    }
    

    You could do in your JS controller something like this to get the text value of your textarea

    $myTextArea.val().strip(); //Where val() returns the html and strip() returns the text
    

    Hope this will help  

提交回复
热议问题