I have some <div>
's with class=tgg
inside the contentEditable <div>
.
the <div>
's with class=tgg
are also contentEditable
(it is required).
The problem occurs when user removes that particular .tgg
div's content. It seems like that div is removed; But after starting typing a span is created in chrome with same style as that of .tgg
and wraps the typed text in it. which produces undesirable effects that should be avoided.
Here is My code:
HTML:
<div id="tarea" contentEditable="true" class="tarea" autocorrect="false" spellCheck="off">
<div class="tgg">Vedant Terkar</div>
Creates another <div class="tgg">Fiddle</div>
for all <div class='tgg'>SO</div> Users.
</div>
<textarea id="opc" rows="5" cols="97">
</textarea>
CSS:
.tarea{
width:99%;
height:300px;
overflow:auto;
clear:both;
display:inline-block;
border:1px solid #000;
padding:5px;
outline:none;
color:#333;
}
.tarea .tgg{
background:#99d;
color:#fff;
padding:1px;
border:1px solid #77b;
display:inline-block;
}
JS:
$(document).ready(function(){
$("#tarea").keyup(function(e){
var html=$(this).html().toString().trim();
html=html.split("<br>").join("\n");
html=html.split(" ").join(" ");
$("#opc").val(html);
$(this).find(".tgg").each(function(){
$(this).attr("contenteditable","true");
});
});
$("#tarea").trigger("keyup");
});
Here is what I'm trying to say:
Here is output of the code as you can see:
When you put your cursor(caret) between fiddle
and for
and remove the fiddle
using BackSpace or Del It is removed as follows:
But now if you start typing any thing it'll not be produced as plain text but it'll be inside a <span>
like:
I'm using chrome
. output may vary for different browser
. Can anyone tell me how can i fix this problem?
any suggestion is welcome. Thanks in advance :)!