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
https://forums.phpfreaks.com/topic/159562-focus-at-the-given-text-into-a-textarea/
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]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.