select text between pre tags |
select text between pre tags |
jackmaessen |
Nov 18 2014, 11:12 AM
Post
#1
|
Novice Group: Members Posts: 25 Joined: 17-January 13 Member No.: 18,471 |
Hello members,
I have this question: i have a case in a javascript file which gives me the oppertunity to create a preprinted text: CODE <pre lang="javascript line="1">code here</pre> I have this piece of code: CODE case 'js': var Codehere = 'code here'; $(TextArea).insertRoundTag('pre', htmlOpts, {lang:'javascript', line:'1'}) $(TextArea).insertTextValue(Codehere, htmlCode); break; The only thing i need now is to get the text code here selected (blue, same situation when you select piece of text with mouse) How can i do that? This post has been edited by jackmaessen: Nov 18 2014, 11:13 AM |
Christian J |
Nov 18 2014, 12:34 PM
Post
#2
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
Last time I checked this was a pretty hard thing to do with ordinary HTML elements (like PRE). See http://quirksmode.org/dom/range_intro.html for an (old) introduction.
It's much easier to select text in a TEXTAREA element: CODE <script type="text/javascript"> function select_textarea() { var textarea=document.getElementById('textarea'); var initial_textarea_length=textarea.value.length; textarea.value+='code here'; textarea.selectionStart=initial_textarea_length; textarea.selectionEnd=textarea.value.length; textarea.focus(); } </script> <textarea id="textarea" cols="20" rows="10">lorem ipsum</textarea> <input type="button" value="Select textarea" onclick="select_textarea();"> (IE8 and older needs a more complicated workaround). The above only selects the recently added text, not any existing textarea content. To select everything always, use CODE textarea.selectionStart=0; instead. |
jackmaessen |
Nov 18 2014, 04:15 PM
Post
#3
|
Novice Group: Members Posts: 25 Joined: 17-January 13 Member No.: 18,471 |
Thanks Christian, that was really helpfull. I am going to play a little bit with this to insert it in my site
|
Lo-Fi Version | Time is now: 19th April 2024 - 07:17 AM |