HTMLTextAreaElement. prototype. getCaretPosition = function (){ // Return the caret position of the textarea Return this. selectionStart; }; HTMLTextAreaElement. prototype. setCaretPosition = function (position ){ // Change the caret position of the textarea This. selectionStart = position; This. selectionEnd = position; This. focus (); }; HTMLTextAreaElement. prototype. hasSelection = function (){ // If the textarea has selection then return true If (this. selectionStart = this. selectionEnd ){ Return false; } Else { Return true; } }; HTMLTextAreaElement. prototype. getSelectedText = function (){ // Return the selection text Return this. value. substring (this. selectionStart, this. selectionEnd ); }; HTMLTextAreaElement. prototype. setSelection = function (start, end ){ // Change the selection area of the textarea This. selectionStart = start; This. selectionEnd = end; This. focus (); }; Var textarea = document. getElementsByTagName ('textarea ') [0]; Textarea. onkeydown = function (event ){ // Support tab on textarea If (event. keyCode = 9) {// tab was pressed Var newCaretPosition; NewCaretPosition = textarea. getCaretPosition () + "". length; Textarea. value = textarea. value. substring (0, textarea. getCaretPosition () + "" + textarea. value. substring (textarea. getCaretPosition (), textarea. value. length ); Textarea. setCaretPosition (newCaretPosition ); Return false; } If (event. keyCode = 8 ){ // Backspace If (textarea. value. substring (textarea. getCaretPosition ()-4, textarea. getCaretPosition () = ""){ // It's a tab space Var newCaretPosition; NewCaretPosition = textarea. getCaretPosition ()-3; Textarea. value = textarea. value. substring (0, textarea. getCaretPosition ()-3) + textarea. value. substring (textarea. getCaretPosition (), textarea. value. length ); Textarea. setCaretPosition (newCaretPosition ); } } If (event. keyCode = 37) {// left arrow Var newCaretPosition; If (textarea. value. substring (textarea. getCaretPosition ()-4, textarea. getCaretPosition () = ""){ // It's a tab space NewCaretPosition = textarea. getCaretPosition ()-3; Textarea. setCaretPosition (newCaretPosition ); } } If (event. keyCode = 39 ){ // Right arrow Var newCaretPosition; If (textarea. value. substring (textarea. getCaretPosition () + 4, textarea. getCaretPosition () = ""){ // It's a tab space NewCaretPosition = textarea. getCaretPosition () + 3; Textarea. setCaretPosition (newCaretPosition ); } } } |