Jump to content

focus at the given text into a textarea


aliento

Recommended Posts

Hi ,

I have a big textarea i have found the code to change and select the changed text with a button but it doesnt focus to the selected text. It shows the beginning of the text area.

Here is the js code :

<script type="text/javascript">
function showMatch(el, newText){
var t= el.value;
var lp = t.split(newText)[0].length;
el.selectionStart = lp
el.selectionEnd = lp + String(newText).length;

}
function SpotPlace(el,tag){

var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);
if(selectedText!=''){
var newText='<'+tag+'>'+selectedText+'</'+tag+'>';
el.value=el.value.replace(selectedText,newText);
}

showMatch(el, newText);
} ;

</script>

 

I need a line that will show the highlighted text not the beginning of the text area.

Link to comment
Share on other sites

I think this is quite a pain to do crossbrowser, for mozilla it's

textarea.selectionStart;

textarea.selectionEnd;

 

Hope this is of some help, it's part of a greasemonkey script I wrote which seems to be doing something similar as to what you're after.

[code]if( document.getElementById( 'topic-page' ) )
{
  if( document.getElementById( 'postform' ) )
  {
    var ctrlDown = false;

    $('post_content').addEventListener( 'keydown', function( e ) { 
      if( e.ctrlKey )
      {
         var start = $('post_content').selectionStart;
         var end   = $('post_content').selectionEnd;
         if( e.keyCode == 66 )
         {
           $('post_content').value = strIntoStr( $('post_content').value, start, '[b]', end, '[/b]' );
           $('post_content').selectionStart = start+3;
           $('post_content').selectionEnd   = end+3;
           e.preventDefault();
         }
      }
    }, false );
  }
}
function $( id )
{
  return document.getElementById( id );
}

[/code]

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.