473,386 Members | 1,698 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,386 software developers and data experts.

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 6795
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="surroundText(this.form.elements['ta'])"
value="Surround text">
</form>

<script type="text/javascript">
function surroundText(ta){
var rng, r2;
if(document.selection &&
document.selection.type=="Text"){
rng=document.selection.createRange();
r2=rng.duplicate();
if(rng.parentElement()==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.asp>
<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.selection &&
document.selection.type=="Text"){
rng=document.selection.createRange();
r2=rng.duplicate();
if(rng.parentElement()==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 "onbeforedeactivate" as a trigger to store the caret position.
<form>
<textarea name="ta"
onbeforedeactivate="storeCaret(this)"></textarea>
<input type="button"
onclick="surroundText(this.form.elements['ta'])"
value="Surround text">
</form>

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

function surroundText(ta){
if(ta.currentRange)
with(ta.currentRange)
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
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...
3
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...
4
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...
2
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...
1
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.