473,765 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cursor in textarea

Hi

Is there posibility to (and how to do this):
a) add a string in the current cursor position?

b) add string before and after selection in textarea e.g. selection

c) if I put with value+='' how to move cursor to position after ] and
before [
Thanks a lot for all help.

Krzysztof Kujawski
Jul 20 '05 #1
3 6850
Krzysztof Kujawski wrote:
a) add a string in the current cursor position?
See the article by Martin Honnen
<URL:http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130>
b) add string before and after selection in textarea e.g. selection
c) if I put with value+='' how to move cursor to position after ] and
before [


<form>
<textarea name="ta"></textarea>
<input type="button"
onclick="surrou ndText(this.for m.elements['ta'])"
value="Surround text">
</form>

<script type="text/javascript">
function surroundText(ta ){
var rng, r2;
if(document.sel ection &&
document.select ion.type=="Text "){
rng=document.se lection.createR ange();
r2=rng.duplicat e();
if(rng.parentEl ement()==ta){
rng.text=""+rng.text+ "";
r2.select(); //to select before
//rng.select(); //to select after
}
}
}
</script>
Ranges are a very useful tool, unfortunately still remaining poorly
implemented across user agents. The example given uses IE ranges and
works only on IE; there is, currently, no way to achieve the same effect
in other browsers (although Mozilla does implement W3C ranges quite well).

<URL:http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/reference/objects/obj_textrange.a sp>
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html>
HTH
Yep.
Jul 20 '05 #2
> <script type="text/javascript">
function surroundText(ta ){
var rng, r2;
if(document.sel ection &&
document.select ion.type=="Text "){
rng=document.se lection.createR ange();
r2=rng.duplicat e();
if(rng.parentEl ement()==ta){
rng.text=""+rng.text+ "";
r2.select(); //to select before
//rng.select(); //to select after
}
}
}
</script>

Everything works fine, but how should I change this above to put
also then when there is only cursor, no selection of text?

Krzysztof Kujawski
Jul 20 '05 #3
Krzysztof Kujawski wrote:
Everything works fine, but how should I change this above to put
also then when there is only cursor, no selection of text?


The approach is a bit different; if you have no selection in the
textarea, then you cannot use the selection property directly since it
will change according to the user actions.

What you need to do is therefore to store the caret position, as
demonstrated in Martin Honnen's article. If targeting only IE5.5+, you
can also use "onbeforedeacti vate" as a trigger to store the caret position.
<form>
<textarea name="ta"
onbeforedeactiv ate="storeCaret (this)"></textarea>
<input type="button"
onclick="surrou ndText(this.for m.elements['ta'])"
value="Surround text">
</form>

<script type="text/javascript">
function storeCaret(ta){
var d=document;
if(d.selection &&
d.selection.cre ateRange) {
ta.currentRange =d.selection.cr eateRange();
}
}

function surroundText(ta ){
if(ta.currentRa nge)
with(ta.current Range)
text=""+text+"";
}
</script>
HTH
Yep.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4914
by: PeP | last post by:
Good morning, I have a form containing a text-area, I'd like to know if it exists a function that, when I activate an event, returns the position of the cursor in the text-area. For example, I write ten words in the text-area, then I move the cursor at the beginning of the second word and activate the event (for example, click on a button): is there a way to know that the cursor is at the beginning of the second word?
3
7443
by: opt_inf_env | last post by:
Hello, I have created a form with a text-fields. <textarea name="explanation" rows=6 cols=70> </textarea> What I don't like there is that if I click in the middle or end of one of the fields cursor is set in position of 16 simbols from the beginning of field. As if there is already 16 blank spaces in the
4
3542
by: Christopher Finke | last post by:
I am writing a Web-based text editor that will be used mainly for editing code. To facilitate this task, whenever the Enter button is pressed within the textarea, the event is caught and the next line is automatically tabbed to match the previous line. However, because I have caught the event and manually inserted the line feed along with the appropriate number of tabs, the textarea doesn't scroll automatically if the line feed puts the...
2
2468
by: Michael | last post by:
Question 1 ---------------- I am writing an advanced BBCode system for my forums and I would like to be able to find where the cursor was positioned last in the text so I could insert the BBCode there. Question 2 ---------------- Again I am writing an advanced BBCode system for my forums and I would like to make is so that when someone puts in a tag it goes bold, so
1
2033
by: arash | last post by:
Hi! I am trying to write some ajax based IDE. I just cant get a div to open where the cursor in the textarea is blinking (in order to suggest possibilities..) Is there a way to get the exact position (pixel-top, pixel-left) of the cursor of a text area?? greetings, arash
1
1332
by: Nivetha | last post by:
i have a list box and one textarea i am trying to pull the tags from list box (on click on the tags of my list box or onclick of a button )into my textarea whereever the cursor is focused but without clicking my textarea its adding only in the first.if i click in the textarea it adding properly. function setCursorPos() { globalCursorPos = getCursorPos(FileColumnData.messageText); } function getCursorPos(textElement) {
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.