I have a div with an attribute contenteditable
and I want so cut the content that's input and is longer than e.g. 5 chars. But the focus is set to start of the input all the time...
var maxlength = 5;
$(".test").attr("contenteditable", function(){
$(this).on('keyup', function( e ){
if( $(this).html().length > (maxlength)){
var content = $(this).html();
content = content.substring(0,(maxlength));
$(this).html(content).focus();
}
});
return true;
});
div{
width: 100px;
height: 20px;
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test"></div>
How can I set the cursor to the end of the div content?
I found some samples here in SO but they all did not work for my construct.
I have tried .focus()
and also with .blur()
before and cleared content... no results...
Of course I don't want to install a Plugin...