By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,661 Members | 1,917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,661 IT Pros & Developers. It's quick & easy.

manipulation of textarea selection

kendall
P: 4
I'm currently working on a PHP CMS for my school that uses simple forms to update the pages and database. To get it out in use, it will have to be usable by people who don't know that [ b ] is bold, and all the rest of it. I have noticed that vBulletin has the best formatting buttons (and no, I'm not talking about the WYSIWYG editor, I know that's way beyond me) so I was thinking of something similar (if I get bold, italic and underlined working, I'm happy). Just for testin, and based on a few things I read about selection, I have the following:
[HTML]
// JS:
function getsel(){
var sel = document.getElementById('editbox').selection;
alert(sel.type);
}
// HTML:
<textarea .... id="editbox"></textarea>
<input type="button" value="click" onclick="getsel();">
[/HTML]

I know that won't do much, bit it should at least say "Text" or something (presuming that I have written and selected text in the textarea), but it doesn't. In the error console (both in Opera and FF), it says something along the lines of "sel has no properties". replacing

[HTML]
alert(sel.type);
with
alert(sel);
[/HTML]

Gives 'undefined', and
[HTML]alert(sel.value);[/HTML]
works, giving whatever happens to be written in it.

So something's wrong...

If anyone has a good howto, or can be bothered to explain this whole selection business to me, it would be much appreciated. Basically what I want to do is take the selection (or, if there is none, the location of the cursor) and stick stuff around it... As you can see, JS isn't my great strength...

Thanks,
-K
Sep 29 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027

kendall
P: 4
Thanks, I'll read through that.

-K
Oct 1 '07 #3

kendall
P: 4
just for reference, I found an excellent script for the job here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=400585
Oct 5 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.