Сообщение от DeV1doR
|
Например, число 12356, бац кликаю мышкой между 3 и 5, жму 4(на кальке) и вуаля 123456.
|
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<input id="res" name="" value="12356">
<input id="ok" name="" type="button" value="4">
<script>
document.querySelector("#ok").onclick = function() {
var target = document.querySelector("#res"),
text = target.value,
caretPos = 0;
if (document.selection) {
target.focus();
var sel = document.selection.createRange();
sel.moveStart("character", -target.value.length);
caretPos = sel.text.length
} else if (target.selectionStart || target.selectionStart == "0") caretPos = target.selectionStart;
target.value = text.substr(0, caretPos) + this.value + text.substr(caretPos);
caretPos++
if (target.setSelectionRange) {
target.focus();
target.setSelectionRange(caretPos, caretPos)
} else if (target.createTextRange) {
var range = target.createTextRange();
range.collapse(true);
range.moveEnd("character", caretPos);
range.moveStart("character", caretPos);
range.select()
}
};
</script>
</body>
</html>