Trying to create a simple html editor.
$('#edit').on('input', function(e){
console.log($(this).html());
});
.edit{
background:gold;
min-height:140px;
font-size:1.6em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='edit' id='edit' contenteditable></div>
type this inside the div edit
:
lorem
ipsum
then place the cursor at the end of lorem
- and press spacebar
and del
on keyboard to get this:
lorem ipsum
and you'll see the problem - ipsum
has much larger font size then lorem
.
That's the first question - how to avoid this?
My second try was using textarea
instead of contenteditable div but in that case I cannot use document.execCommand
to change some text to be bold
etc.
So what is the way to set html tags inside textarea
like it's done here on stackoverflow
, or on wordpress
sites and so on?
My final goal is to provide a simple editor with just a a few commands - bold, italic and align
Users should be able to type and style the text, and click on a button should get the html content.
Any help?