I am working on an HTML 5 editor that utilizes the contenteditable
tag. Inside this tag, I have a span
. The issue arises when all text is removed as the span also gets removed. I want to prevent the removal of the span, how can I achieve this?
Here is a snippet of my code:
$('#editor').keyup(function(){
$('#spanText').text($(this).find('span').text());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div contenteditable="true" id="editor"><span>This is my text</span></div>
<br><br><hr>
<div id="spanText"></div>