By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Simulate keypress

P: n/a
Hello,

As the subject says it, is there a way to simulate a special keypress in JS
? In my case, on loading an HTML page, i'd like to set the cursor at the end
of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
First of all sorry about my poor English, iŽll try to do the best.

Well, iŽm having a similar problem because i wanŽt to select certaint
characters from a text box. Maybe this routine iŽve just created , solves
your problem.

This routine, selects in a textbox characters from StartIndex to EndIndex.
If you gives to the routine both parameters with textbox content lenght, i
think it puts the cursor in the desired position.

function selectFromTo (textInput, charStartIndex, charEndIndex) {

/* WeŽll calculate the appropiate value to the different moves. */
strTextInputValue = "" + textInput.value;

charStartIndex = (parseInt(charStartIndex) - 1);

numEndIndex = parseInt(charEndIndex);
numEndIndex -= strTextInputValue.length;
charEndIndex = numEndIndex.toString();

if (textInput.createTextRange) {
var range = textInput.createTextRange();
range.moveStart('character', charStartIndex );
range.moveEnd('character', charEndIndex);
range.select();
}
}

Raul Carrillo a.k.a Metsuke
"Christian" <cg********@yahoo.com> escribió en el mensaje
news:bo**********@reader1.imaginet.fr...
Hello,

As the subject says it, is there a way to simulate a special keypress in JS ? In my case, on loading an HTML page, i'd like to set the cursor at the end of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.

Jul 20 '05 #2

P: n/a
"Raul Carrillo a.k.a. Metsuke <(^_^)>" <rc*******@coleccionalo.com> wrote in
message news:9i**********************@telenews.teleline.es ...
First of all sorry about my poor English, iŽll try to do the best.

Well, iŽm having a similar problem because i wanŽt to select certaint
characters from a text box. Maybe this routine iŽve just created , solves
your problem.

This routine, selects in a textbox characters from StartIndex to EndIndex.
If you gives to the routine both parameters with textbox content lenght, i
think it puts the cursor in the desired position.

function selectFromTo (textInput, charStartIndex, charEndIndex) {

/* WeŽll calculate the appropiate value to the different moves. */
strTextInputValue = "" + textInput.value;

charStartIndex = (parseInt(charStartIndex) - 1);

numEndIndex = parseInt(charEndIndex);
numEndIndex -= strTextInputValue.length;
charEndIndex = numEndIndex.toString();

if (textInput.createTextRange) {
var range = textInput.createTextRange();
range.moveStart('character', charStartIndex );
range.moveEnd('character', charEndIndex);
range.select();
}
}

Raul Carrillo a.k.a Metsuke
"Christian" <cg********@yahoo.com> escribió en el mensaje
news:bo**********@reader1.imaginet.fr...
Hello,

As the subject says it, is there a way to simulate a special keypress in

JS
? In my case, on loading an HTML page, i'd like to set the cursor at the

end
of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.


Thanks for the reply. I eventually found a similar answer on google, using
createTextRange. That will solve my problem, though i doubt this will work
on every browser.

For a more general matter, is there a way to simulate a keypress ?
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.