aliento Posted May 25, 2009 Share Posted May 25, 2009 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. Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 25, 2009 Share Posted May 25, 2009 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.