The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> select text between pre tags
jackmaessen
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jackmaessen
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 07:17 AM