Adsense
Popular Posts
- Code update from PHP 7.4 to PHP 8.1 - PhpSpreadsheet
- MySQL workbench -"Could not decrypt password cache"
- Code update from PHP 7.4 to PHP 8.1 - Worksheet/Iterator.php
- Rendering HTML tags inside textarea
- axios handle blob type data
- Unix Utils and wget in Windows
- increase mysql query speed
- Setup vi syntax for PHP
- Get WAMP running on EC2
- EXCEL return to the next line in the same cell
Thursday, January 3, 2019
Move cursor at end after paste in jQuery
Using $(this).html(textstring1) in JavaScript, the cursor will be at the top.
The following code is to move cursor to the end:
$.fn.focusEnd = function() {
$(this).focus();
var tmp = $('<span />').appendTo($(this)),
node = tmp.get(0),
range = null,
sel = null;
if (document.selection) {
range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
range = document.createRange();
range.selectNode(node);
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
tmp.remove();
return this;
}
Example:
function clickother_cflnew($td){
$.fn.focusEnd = function() {
$(this).focus();
var tmp = $('<span />').appendTo($(this)),
node = tmp.get(0),
range = null,
sel = null;
if (document.selection) {
range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
range = document.createRange();
range.selectNode(node);
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
tmp.remove();
return this;
}
$td.find('.cfl_new_other').on('paste', function(event){
event.preventDefault();
var text = (event.originalEvent || event).clipboardData.getData('text/plain') || prompt('Paste something..');
window.document.execCommand('insertText', false, text);
textstring = $(this).html();
textstring1 = textstring.replace(/<div>/g, '<br>').replace(/<\/div>/g, '');
textstring1 = textstring1.replace(/ /g, ' ');
$(this).html(textstring1);
// $(this).html($(this).html().replace(/<div>/g, '<br>').replace(/<\/div>/g, '<br>'));
$(this).focusEnd();
});
$td.find('.cfl_new_other').unbind('keyup change input').bind("keyup change input",function() {
el = $(this);
textstring = el.html();
textstring1 = textstring.replace(/<div>/g, '<br>').replace(/<\/div>/g, '');
textstring1 = textstring1.replace(/ /g, ' ');
if(textstring1.length >= 500){
alert("Maximum 500 characters");
el.html( textstring1.substr(0, 500) );
el.focusEnd();
$(this).parent().find(".charNum").text( " 0 character remaining.");
} else {
$(this).parent().find(".charNum").text((500-textstring1.length) + " characters remaining.");
}
});
}
Labels:
jQuery
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment