Textarea

S

Simon

How can I recognize the position of the cursos in a textarea?
I want to now when the cursor is at the beginning of the textarea and when
the cursor is at the end of the textarea.

(In combination with event.keyCode i can use the arrow up to go back to the
field above the textfield IF the cursor is at the beginning of the text and
with the arrow down i can go to the next field, IF the focus is at end of
the text)
Thanks,
Simon
 
S

Simon

So, give me the answer than. It should be about 2 lines of code, in stead of
200.
(var pos=text.focus.position something like that? Isn't it build in?
 
E

Evertjan.

Simon wrote on 10 jul 2007 in comp.lang.javascript:
[Please do not toppost on usenet]
So, give me the answer than.

Simon, this NG is not a paid helpdesk.

We, at least most of us, expect you to read past answers,
before asking the same in this NG.
It should be about 2 lines of code, in stead of 200.

How do you know?
 
S

Simon

Sorry, i've already found the solution.

So here's the answer to my own question:

<html>
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function doGetCaretPosition (ctrl) {

var CaretPos = 0;
// IE Support
if (document.selection) {

ctrl.focus ();
var Sel = document.selection.createRange ();

Sel.moveStart ('character', -ctrl.value.length);

CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;

return (CaretPos);

}


function setCaretPosition(ctrl, pos)
{

if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}

function process()
{
var no = document.getElementById('no').value;
setCaretPosition(document.getElementById('get'),no);
}

</script>
</head>
<body>
<textarea id="get" name="get" rows="5" cols="31">Please write some integer
in the textbox given below and press "Set Position" button. Press "Get
Position" button to get the position of cursor.</textarea>
<br>
Enter Caret Position: <input type="text" id="no" size="1" /><input
type="button" onclick="process();" value="Set Position">
<BR>
<input type="button"
onclick="alert(doGetCaretPosition(document.getElementById('get')));"
value="Get Position">
</body>
</html>

Voila

Evertjan. said:
Simon wrote on 10 jul 2007 in comp.lang.javascript:
[Please do not toppost on usenet]
So, give me the answer than.

Simon, this NG is not a paid helpdesk.

We, at least most of us, expect you to read past answers,
before asking the same in this NG.
It should be about 2 lines of code, in stead of 200.

How do you know?
(var pos=text.focus.position something like that? Isn't it build in?
 
E

Evertjan.

Simon wrote on 11 jul 2007 in comp.lang.javascript:
Sorry, i've already found the solution.

Is finding a solution something to be sorry about?

Why do you toppost,
so having us to mangle trough your posting to find the question?

Two lines, you sure? Then the below is not your solution.
So here's the answer to my own question:

<html>
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function doGetCaretPosition (ctrl) {

var CaretPos = 0;
// IE Support
if (document.selection) {

ctrl.focus ();
var Sel = document.selection.createRange ();

Sel.moveStart ('character', -ctrl.value.length);

CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;

return (CaretPos);

}


function setCaretPosition(ctrl, pos)
{

if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}

function process()
{
var no = document.getElementById('no').value;
setCaretPosition(document.getElementById('get'),no);
}

</script>
</head>
<body>
<textarea id="get" name="get" rows="5" cols="31">Please write some
integer
in the textbox given below and press "Set Position" button. Press "Get
Position" button to get the position of cursor.</textarea>
<br>
Enter Caret Position: <input type="text" id="no" size="1" /><input
type="button" onclick="process();" value="Set Position">
<BR>
<input type="button"
onclick="alert(doGetCaretPosition(document.getElementById('get')));"
value="Get Position">
</body>
</html>

Voila

Evertjan. said:
Simon wrote on 10 jul 2007 in comp.lang.javascript:
"Evertjan." <[email protected]> schreef in bericht
Simon wrote on 10 jul 2007 in comp.lang.javascript:

How can I recognize the position of the cursos in a textarea?

Since 2000 this has been discussed over a 100 times:

<http://groups.google.com/groups/search?
q=textarea+cursor.position+group:comp.lang.javascript>

[Please do not toppost on usenet]
So, give me the answer than.

Simon, this NG is not a paid helpdesk.

We, at least most of us, expect you to read past answers,
before asking the same in this NG.
It should be about 2 lines of code, in stead of 200.

How do you know?
(var pos=text.focus.position something like that? Isn't it build in?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top