Home >Web Front-end >JS Tutorial >js code to insert value at cursor position_javascript skills
/** <br>*Insert a string into the input field (where the cursor is) <br>*@param $t document.getElementById('fieldId') <br>*@param myValue The value to be inserted<br>* * <br>function addSplitToField($t,myValue){ <br>if (document.selection) { <br>$t.focus(); <br>sel = document.selection.createRange(); <br>sel .text = myValue; <br>$t.focus(); <br>}else if($t.selectionStart || $t.selectionStart == '0') { <br>var startPos = $t.selectionStart; <br>var endPos = $t.selectionEnd; <br>var scrollTop = $t.scrollTop; <br>$t.value = $t.value.substring(0, startPos) myValue $t.value.substring(endPos , $t.value.length); <br>this.focus(); <br>$t.selectionStart = startPos myValue.length; <br>$t.selectionEnd = startPos myValue.length; <br>$t. scrollTop = scrollTop; <br>}else{ <br>$t.value = myValue; <br>$t.focus(); <br>} <br>} <br>