-
July 5th, 2006, 04:37 AM
#1
TextArea Selection
Hello guys!
I know I can use .select() to select all text, but what I want to do is:
I have a TextArea. In that, the user highlights a word (it can be any word), then he clicks on a button to add a <B> tag in front of the selected word, and a </B> tag after the selected word.
How can I get the user's selected word in this case ¿
Any help will be greatly appreciated.
-
July 5th, 2006, 05:13 AM
#2
Re: TextArea Selection
I've not dabbled much into this, so I don't know if it is this you need, but...
FireFox there are two javascript functions called "selectionStart" and "selectionEnd" which will give you the indexes of the respective start and end of a selection.
In Internet Explorer I only know if using "document.selection.createRange().text;" which seems to work.
Hope this helps
-
July 5th, 2006, 05:32 AM
#3
Re: TextArea Selection
Hello Alsvha!
Thanx for replying!
You see, I'm accustomed to have a SelectionStart and SelectionEnd type functions in VB and other programming languages, so that was the first things I searched for.
It's the very first time I hear about document.selection.createRange().text;, I'll do some research on it and will let you know asap.
Thanx once again!
-
July 5th, 2006, 05:51 AM
#4
Re: TextArea Selection
There are likely other proberties on document.selection.createRange() because it returns an object.
As said though - I've not looked much into these in the past, so I can't guide you much.
However, this seems to be the entry point on microsofts pages:
http://msdn.microsoft.com/workshop/a..._selection.asp
-
July 5th, 2006, 06:18 AM
#5
Re: TextArea Selection
Hello again Alsvha, you have helped me a lot!!
All I needed was document.selection.createrange.
I (you) have fixed my problem, and just for interest sake, I'm supplying the code here.
The purpose of this script is to add <b> and </b> tags around a selection in the TextArea.
PHP Code:
<html>
<head>
<title>Add Formatting tags!</title>
<script type="text/javascript">
function makeBold() {
theSelection = document.selection.createRange().text;
if (!theSelection)
return false;
if (document.selection.createRange().parentElement().tagName != 'TEXTAREA')
return false;
document.selection.createRange().text = "<b>" + theSelection + "</b>";
}
</script>
</head>
<body>
<textarea rows = 10 cols = 50></textarea>
<input type = Button value = "Click" onClick="makeBold()">
</body>
</html>
Works like a charm!!
Thanx once again!
-
July 5th, 2006, 06:53 AM
#6
Re: TextArea Selection
Glad I was able to help
-
November 17th, 2006, 07:07 PM
#7
Re: TextArea Selection
Oh this worked a treat. You sir are a star.
I had been looking for this feature for 2 days or more, searching under every word I could think of.
Now here's a tricky one, how could this be adapted to URLs. Say you select the text, then click a url button, a pop up box apears with a space to enter the url, then you hit Ok or Submit and it adds the code/tags to the box.
Could that be done?
Oh and I only registered to to say how wicked that code is!
-
November 18th, 2006, 12:37 PM
#8
Re: TextArea Selection
Welcome to the forums laughingbuddha. Please remember that when you have a new question to post it in a new thread.
If you are talking about typing a URL into a popup box and then going to that URL, just using prompt() and then window.location, or location.replace().
If you want to get the selection from the URL line in the browser, that cannot be done.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|