Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using <br> instead of <p> in Summernote?

Needed to use <br> tag in the summernote editor instead of <p> while user clicks on the Enter button, so here is my code:

var $this = $(this),
    box = $('textarea.CommentsFields');
box.summernote({
    height: 100,
    focus: true,
    toolbar: [
        [ 'all', [ 'bold', 'strikethrough', 'ul', 'ol', 'link' ] ],
        [ 'sided', [ 'fullscreen' ] ]
    ],
    callbacks: {
        onEnter: function(){
            box.summernote('insertNode', document.createTextNode("<br>")); 
            console.log('uiwdbvuwecbweuiuinsjk');
        }
    }
});

I wrote a custom callback of the onEnter event, when the user hit the return button it raises a callback, and write the <br> tag which is not what I am looking for.

result screenshot

I read their documentation but can not understand how to stop the default action of the enter button and write <br> tag instead of wrapping the element in <p> tag.

Any idea? Thanks

like image 635
rakibtg Avatar asked May 12 '16 11:05

rakibtg


People also ask

How do you customize Summernote?

You can customize it with popover. air option. $('#summernote'). summernote({ popover: { air: [ ['color', ['color']], ['font', ['bold', 'underline', 'clear']] ] } });

How do I get plain text from Summernote?

html(text). text(); . Because if you the content of summernote contain some plain text and you don't wrap the whole content inside an HTML tag(like div) then, jQuery will through an error saying it was not expected.

How do I turn off textarea in Summernote?

You can disable editor by API. $('#summernote'). summernote('disable'); If you want to enable editor again, call API with enable.


2 Answers

This code worked for me:

$("#summernote").on("summernote.enter", function(we, e) {
     $(this).summernote("pasteHTML", "<br><br>");
     e.preventDefault();
});

This interceps the Enter press event and changes its default behaviour, inserting a <br> instead of a new paragraph.

like image 165
EstevaoLuis Avatar answered Oct 01 '22 05:10

EstevaoLuis


If you don't want to change or fix the summernote library itself, you can use the shortcut keys for adding a line break.

  • Use Shift + Enter for giving a line break.
  • Use Enter for changing a paragraph, as summernote add a div/p to start a new line when you press Enter.

Hope this works.

like image 37
Shahrukh Anwar Avatar answered Oct 01 '22 06:10

Shahrukh Anwar