selections within textareas

This is a discussion on "selections within textareas" within the JavaScript Forum section. This forum, and the thread "selections within textareas are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 15th, 2004, 18:27
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
selections within textareas

i really didn't know where to put this!

its done on a lot of sites (including this one!), but how is it done? how can you detect what is highlighted in a textarea, and then add text around it? for example, they can highlight a word and click bold and it adds bold tags around it...?

how?!

thanks

bacardi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Oct 16th, 2004, 07:48
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
using the textRange object....
haven't done it in a while, lemme find it for you...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 16th, 2004, 07:54
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
this should do:
http://msdn.microsoft.com/library/de.../textrange.asp

http://msdn.microsoft.com/library/de..._textrange.asp
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 16th, 2004, 19:26
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
ok thanks spinal but i never understand the msdn language - its too technical for me! Do you think you could explain it to me in more simple terms? much appreciated if you can...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 17th, 2004, 12:22
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,612
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
This is my code to make the highlited text to be a url link..
Just put this on the <head> tag...
and call them using an gif or a text.

Code: Select all
function hiliteToExternLink(txtArea){
    txtAreaName   = txtArea.name;
    txtRange      = document.all[txtArea].createTextRange();
    txtContainer  = txtRange.parentElement().name;
    
    objRange      = document.selection.createRange();
    hiliteTxt     = objRange.text;

    toExternLink   = '' + hiliteTxt + '';
    
    if(hiliteTxt != ""){
        objRange.text = toExternLink;
    }
}
I dont have the full code now...
I'll post to you later, ok
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 17th, 2004, 13:55
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
thanks monie... it would be good if you could post the full code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 17th, 2004, 15:08
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
I'm not good at these things but i'll explain monie's code...

THE IDEA:
the textRange object lets you rerieve and change text within an element.

USES:
"document.selection" points to the current selection on the document (duh!)

I made a small example so you can try it out yourself.
save this onto a htm file and run it:
Code: Select all
<SCRIPT>
function MakeBold(){
 // find selection
 r = document.selection.createRange();

 // exit function if no selection has been made
 if(r==null){ return; }

 // replace selection by itself, surrounded by the "[b]" tag
 r.pasteHTML("" +r.htmlText+ "");
}
</SCRIPT>
<INPUT TYPE=button value="Make Selection Bold" onMouseDown="MakeBold();">


text and more text and more text and more text and more text and more 
text and more text and more text and more text and more text and more 
text and more text and more text and more text and more text and more 
text and more text and more text and more text and more text and more 
text and more text and more text and more text and more text and more 
text and more text and more text and more text and more text and more 
</BODY>
1 . make a selection
2 . click the "Make Selection Bold" button
3 . that will trigger a function wi=hich will:
3 a) find the selection in the body of the document
3 b) retrieve the selected text
3 c) replace it by the same selected text sorrounded by the "[b]" tag
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 18th, 2004, 03:54
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,612
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Code: Select all
<head>
<SCRIPT language=JavaScript1.2>

function hiliteToBold(txtArea){
	txtAreaName   = txtArea.name;
	txtRange      = document.all[txtArea].createTextRange();
	txtContainer  = txtRange.parentElement().name;

	objRange      = document.selection.createRange();
	hiliteTxt     = objRange.text;
	toBoldTxt     = "" + hiliteTxt + "";
	
	if(hiliteTxt != ""){
		objRange.text = toBoldTxt;
	}
}
</SCRIPT>
<head>
Code: Select all
<body>
<IMG: set your image button here..>


<TEXTAREA name=txtMainText rows=15 cols=30></TEXTAREA>
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 18th, 2004, 16:15
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
thanks guys i've worked it out and written one of my own... ... spinal, was it you who had that rich text editor that you made yourself? if so, how did you make one that updates it automatically like that... if you know what i mean
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 19th, 2004, 10:55
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
<blockquote id="quote" class="ffs">quote:<hr height="1" noshade="noshade" id="quote" />Originally posted by benbacardihow did you make one that updates it automatically like that<hr height="1" noshade="noshade" id="quote" /></blockquote id="quote">
what do you mean?

basics of editors: (as far as I know)
- manipulate the code
- show result so user "thinks" he's changing the text to bold while in fact he's placing "[b]"tags around it

simple implementation:
- use a textarea for the code
- create buttons that will insert whatever formatting to the code (just like the editor in this forum)
- use jscript to write the content of the textarea onto the document so you can see the HTML result. for this, use textRange.pasteHTML([textarea contents]);
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Oct 19th, 2004, 19:00
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
i mean how do you make something like this http://www.dotmagic.co.uk/richeditor
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #12  
Old Oct 20th, 2004, 09:11
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
Ben.... thats mine.. lol
That one sadly only works in internet explorer. If it were for an intra-net where all users had IE, then it would be perfect.

I wrote that one about a year ago and never used it.... it's still unfinished!
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #13  
Old Oct 20th, 2004, 12:17
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
I would not advice you designing your own HTML editor for the following reasons:

1. there's free stuff out there. many very talented programmers (God bless the bastards) spend months or even years developing the most sweet pieces of coding you ever seen and let you use at at will. I gave up my own editor when I found htmlArea (http://www.interactivetools.com/products/htmlarea/)

2. having made the point above, you'll be wasiting your time trying to design something that will match the quality of the above, so unless you're simply trying to learn a few techniques, don't go all the way. by all means try and re-create the software in your own way, practice and learn from the code, but anyone is better off taking advantage of the free software available.

3. I forgot wat my third point was...


as posted here:
http://www.webforumz.com/topic.asp?TOPIC_ID=1946
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #14  
Old Jan 12th, 2005, 09:20
Highly Reputable Member
Join Date: Jul 2003
Location: Ipswich, UK
Posts: 690
Thanks: 0
Thanked 0 Times in 0 Posts
ive had problems with HTMLArea, the best ive found is: http://richtext.sourceforge.net/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #15  
Old Feb 15th, 2005, 17:13
Anonymous User
Guest
Posts: n/a
What if you have 2 textareas, how can you determin in which of them the selection has been made?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #16  
Old Feb 16th, 2005, 08:53
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
I dont quite understand what you mean. Each textarea would have an ID. You just reference each textbox by it's id and see which has the selection.
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #17  
Old Feb 16th, 2005, 13:59
Anonymous User
Guest
Posts: n/a
and how do you do that?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Tags
selections, within, textareas

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Navigation from 2 pulldown selections? svoltmer JavaScript Forum 2 Apr 28th, 2008 12:28
Highlighting text selections mikemike2004 JavaScript Forum 6 Mar 15th, 2008 21:24
[SOLVED] color-box selections pesho318i JavaScript Forum 6 Dec 22nd, 2007 20:57
PHP and textareas ktsirig PHP Forum 3 Sep 25th, 2005 22:41


All times are GMT. The time now is 03:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved