473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2428

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
1
by: Rob | last post by:
I have a date text box (input type = text) in an ASP.NET/Javascript environment. I wanted to force the users to enter dates in a "__/__/____", "dd/mm/yyyy" or similar format. The textbox needs to...
9
by: Cerebral Believer | last post by:
Hi folks, I am having some issues using a program that protects my web pages (to a degree) using JavaScript (to print screen/disable clipboard, caching text selection etc). Below is the code...
1
by: Iain | last post by:
I have the following .net page with a pushbutton in a datagrid. This pushbutton should delete the selected record. It works in the same manner in many other pages of the same format (I actually...
3
by: Radu | last post by:
Hi. I have the following problem - I need to build a user-control in asp.net (an ascx) to somehow allow the users to search by first name or last name among a big (~10.000 records) email addresses...
1
by: sva0008 | last post by:
i have a auto suggest script that does not work in firefox , works great on IE. /******************************************************* AutoSuggest - a javascript automatic text input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.