468,133 Members | 1,221 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

does javascript allow you to obtain selected text?

I have a textfield and you of course you can select text...

When they hit the bold button I want it to obtain the selected text and
bold it, the hard part is trying to figure out if javascript can even
OBTAIN selected text?!?!

I can do this in java if I have to, I know java better than javascript,
it seems as that is the only way to do it from looking online.

<-- me javascript beginner

Oct 31 '05 #1
3 2201

go***********@gmail.com wrote:
I have a textfield and you of course you can select text...
When you say a "text field" what exactly do you mean?

A TEXTAREA element? That does not support bold or other formatting.

An INPUT type=text? That does not support bold or other formatting.

A <DIV contentEditable=true>? That is IE only at present. You will
need to research it.

A document with designMode="on"? IE,Firefox,Safari(I think)
When they hit the bold button I want it to obtain the selected text and
bold it, the hard part is trying to figure out if javascript can even
OBTAIN selected text?!?!
Yes it is perfectly possible, but it is browser specific.

In Internet Explorer, research the "selection" and "TextRange" objects,
and the "execCommand" function, on the MSDN web site.

http://msdn.microsoft.com/library/de..._selection.asp

In Mozilla/Firefox you will need to research Midas and "getSelection"

http://www.mozilla.org/editor/midas-spec.html
I can do this in java if I have to, I know java better than javascript,
it seems as that is the only way to do it from looking online.
Try googling on "Rich Text Editor".
<-- me javascript beginner


Good luck, it is a steep-ish learning curve.

Julian

Oct 31 '05 #2
s = document.selection.createRange().text;
s.style.fontWeight = 'bold';

I used this in my java script, it will create bold tags around the text
but not actually make it bold. I'm guessing since you said it doesn't
support having bold characters ... that it cannot be done, which seems
to be the case, I'll look into the other things I suppose.

Nov 1 '05 #3
VK
go***********@gmail.com wrote:
I have a textfield and you of course you can select text...

When they hit the bold button I want it to obtain the selected text and
bold it, the hard part is trying to figure out if javascript can even
OBTAIN selected text?!?!


As it was mentioned above, text field, textarea and other form elements
are "atomic formatting" fragments. It means that you can apply nearly
any formatting to them but to the *entire element only*. So say having
<input type="text" name="txt0" value="Lorem ipsum"> you can make "Lorem
ipsum" text bold, italic, green etc. but you cannot make say "Lorem"
bold and "ipsum" normal. This is how it was made and no programming
language (JavaScript or Java or any other) will change it.

But let's us assume that you just did not express yourselve properly
and you are working with a custom made pseudo-textarea based on <div>
or similar.

In this case you hit the issue that in order to properly apply the
formatting you need *two* pieces of info:
1) Text content fragment to apply formatting to
2) Exact position of this content fragment in the document.

The first data are equally provided by all existing browsers: TextRange
in IE, getSelection() in NN, FF, Safari and others.

Unfortunately the second piece of info provided by IE only via
TextRange properties. Other rivals still suck. It means that in the
context: "Some text text text" you can select first "text" and
setSelection(), but you have no means to know which one of these three
"text" should be displayed bold.

If you don't care about others besides IE, just go ahead with
TextRange.
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_textrange.asp>

Nov 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by dmiller23462 | last post: by
9 posts views Thread by Cerebral Believer | last post: by
1 post views Thread by Iain | last post: by
1 post views Thread by sva0008 | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.