I'm trying to implement some maxlength function on SCEditor, which doesn't take into account original textarea maxlength attribute.
By then I'm using the SelectionchangedEvent handler, to get value and substring it if necessary, and replacing it with val() from SCEditor API.
That works so far but the val() function will set value and place caret at the beginning, so if user continues writing it will write at top then substring the bottom, and set back caret on top, one line above previous one. I don't want that !
So I came across solutions like : stackoverflow
That doesn't seem to work unfornatunately : jsfiddle
var instance = $('textarea').sceditor({
plugins: 'bbcode',
width: '100%',
style: 'http://www.sceditor.com/minified/jquery.sceditor.default.min.css'
}).sceditor('instance');
$('#caretend').click(function() {
placeCaretAtEnd(instance);
});
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el.getBody().get(0));
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el.getBody().get(0));
textRange.collapse(false);
textRange.select();
}
}
<link href="http://www.sceditor.com/minified/jquery.sceditor.default.min.css" rel="stylesheet"/>
<div><a href="#" id="caretend">Place caret at end</a></div>
<div><textarea></textarea></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.sceditor.com/minified/jquery.sceditor.bbcode.min.js"></script>
Am i missing something right here ? Or is it just this plugin in conflict with the function ?
And finally, I also found some interesting post here : stackoverflow but I don't see how to use in it in the intended way.
Any advice very welcome, thanks a lot in advance.