Skip to content Skip to sidebar Skip to footer

Js/jquery: Copy Contenteditable Div To Textarea, Keeping Newline Info

I have a contenteditable div. It starts out with some children. There is also a textarea. When 'enter' is pressed, the contents of the div are copied to the textbox. My problem is

Solution 1:

Change the java script for:

// jquery
$(document).keypress(function(e) {
    if(e.which == 13) {  
        var result = '';
        $('#e > div').each(function (i, e) { 
            result = result + $(e).text().trim() + '\r';
        });
       $('#ta').val(result);
    }
});

Works fine for me! Hopes this help you!

Solution 2:

Use oninput event listner

contenteditableDiv.addEventListener("input", function(){ 

       textAreaElement.value = contenteditableDiv.innerText;

      });

Solution 3:

I think, it's like a inputtext more.. I hope help someone !

functionenteredDiv(cont) {
        $cont.keypress(function (e) {
           var keycode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == 13) {
                e.preventDefault();

                var result = $cont.text().trim();
                $cont.text('');

                e.stopImmediatePropagation();
            }
        });
    }

Post a Comment for "Js/jquery: Copy Contenteditable Div To Textarea, Keeping Newline Info"