Determining the focus point inside the Textarea

I have a textbox ...

<textarea>
Some writing here
*
Then some more on another line
</textarea>

      

What I want to do is configure the user where *. Is it possible?

+2


source to share


2 answers


Use the following function to set the selection in a text box.

function setSelRange(inputEl, selStart, selEnd) { 
   if (inputEl.setSelectionRange) { 
     inputEl.focus(); 
     inputEl.setSelectionRange(selStart, selEnd); 
   } else if (inputEl.createTextRange) { 
     var range = inputEl.createTextRange(); 
     range.collapse(true); 
     range.moveEnd('character', selEnd); 
     range.moveStart('character', selStart); 
     range.select(); 
   } 
}
// From http://www.webmasterworld.com/forum91/4527.htm

      



So, in your case, you can search for the character position *

and use that value in a call like this:

var pos = 17; // Set this to the position of the * character.
setSelRange(document.getElementById('textareaId'), pos, pos);

      

+5


source


to ease your burden.



<textarea id="myarea">
Some writing here
*
Then some more on another line
</textarea>


function FocusMe(what){ // what = character to be focused(in your case *)
 var cFocus = document.getElemenById("myarea").innerHTML;
 var pos = cFocus.indexOf(what);
 setSelRange(document.getElementById('myarea'), pos, pos); //Jame answer above.
}

      

0


source







All Articles