469,355 Members | 2,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,355 developers. It's quick & easy.

Activate (insert)key through javascript

Hi there,

How can i activate, for example, the insertkey, without pressing the key
itself?
Something like: "window.event.keyCode = 45" won't work.

Any ideas?

It should happen onfocus, and "unhappen" "onunfocus"

(I have an inputfield, value is today's date, but when onfocus it should
type over the first characters so that the year wont have to by retyped.
(After adjusting day and month people can leave the field and go to the next
field.)

Thanks,
Simon
Feb 28 '07 #1
5 3532
ASM
Simon a écrit :
>
(I have an inputfield, value is today's date, but when onfocus it should
type over the first characters so that the year wont have to by retyped.
(After adjusting day and month people can leave the field and go to the next
field.)
why not simply leave user fill only day and month
and onblur to add the year to this entry ?

Insteed to try to give something indigestible to browsers.

<input onblur="var v=this.value, d=new Date(), d=d.getFullYear();
if(v.length!=5) alert('please enter date in this format:\ndd/mm');
else this.value= v+'/'+d;">
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Feb 28 '07 #2
Because people must be able to change the year also..
It's within an application I've developped.
Why is it indigestible?
I want it like this ;)

Thanks anyway,
Simon

"ASM" <st*********************@wanadoo.fr.invalidschre ef in bericht
news:45***********************@news.orange.fr...
Simon a écrit :
>>
(I have an inputfield, value is today's date, but when onfocus it should
type over the first characters so that the year wont have to by retyped.
(After adjusting day and month people can leave the field and go to the
next
field.)

why not simply leave user fill only day and month
and onblur to add the year to this entry ?

Insteed to try to give something indigestible to browsers.

<input onblur="var v=this.value, d=new Date(), d=d.getFullYear();
if(v.length!=5) alert('please enter date in this format:\ndd/mm');
else this.value= v+'/'+d;">
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date

Feb 28 '07 #3
On Feb 28, 7:13 am, "Simon" <simon1...@quicknet.nlwrote:
Because people must be able to change the year also..
It's within an application I've developped.
Why is it indigestible?
I want it like this ;)

Thanks anyway,
Simon

"ASM" <stephanemoriaux.NoAd...@wanadoo.fr.invalidschre ef in berichtnews:45***********************@news.orange. fr...
Simon a écrit :
(I have an inputfield, value is today's date, but when onfocus it should
type over the first characters so that the year wont have to by retyped.
(After adjusting day and month people can leave the field and go to the
next
field.)
why not simply leave user fill only day and month
and onblur to add the year to this entry ?
Insteed to try to give something indigestible to browsers.
<input onblur="var v=this.value, d=new Date(), d=d.getFullYear();
if(v.length!=5) alert('please enter date in this format:\ndd/mm');
else this.value= v+'/'+d;">
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date- Hide quoted text -

- Show quoted text -
You can use a method something like this:

function setSelection(element) {
if (element.value.length 0) {
var v = element.value;
if (element.setSelectionRange) {
element.setSelectionRange(0, v.lastIndexOf('/'));
}
else if (element.createTextRange) {
var range = element.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', v.lastIndexOf('/'));
range.select();
}
}
}

Then call it from your input element like this:

<input type="text" id="date1" onfocus="setSelection(this);"/>

The method above assumes you use '/' as a date separator. You can
change it to '-' or whatever as neccessary. Best bet would be to
accomodate for both and check for which separator is used and
reference it. For example:

function setSelection(element) {

if (element.value.length 0) {
var v = element.value;
var separator = '/';
if (v.indexOf('-') 0) {
separator = '-';
}
if (element.setSelectionRange) {
element.setSelectionRange(0, v.lastIndexOf(separator));
}
else if (element.createTextRange) {
var range = element.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', v.lastIndexOf(separator));
range.select();
}
}
}

Or something like that...

Feb 28 '07 #4
Thanks,

Still I find it very strange that you can't activate a key by script. Is
this really impossible??
I can alert de keycode, when a key is pressed, but i can't manually activate
a keycode.
Too bad.

But thanks!

"Tom Cole" <tc****@gmail.comschreef in bericht
news:11**********************@a75g2000cwd.googlegr oups.com...
On Feb 28, 7:13 am, "Simon" <simon1...@quicknet.nlwrote:
Because people must be able to change the year also..
It's within an application I've developped.
Why is it indigestible?
I want it like this ;)

Thanks anyway,
Simon

"ASM" <stephanemoriaux.NoAd...@wanadoo.fr.invalidschre ef in
berichtnews:45***********************@news.orange. fr...
Simon a écrit :
(I have an inputfield, value is today's date, but when onfocus it
should
type over the first characters so that the year wont have to by
retyped.
(After adjusting day and month people can leave the field and go to the
next
field.)
why not simply leave user fill only day and month
and onblur to add the year to this entry ?
Insteed to try to give something indigestible to browsers.
<input onblur="var v=this.value, d=new Date(), d=d.getFullYear();
if(v.length!=5) alert('please enter date in this format:\ndd/mm');
else this.value= v+'/'+d;">
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date- Hide quoted
text -

- Show quoted text -
You can use a method something like this:

function setSelection(element) {
if (element.value.length 0) {
var v = element.value;
if (element.setSelectionRange) {
element.setSelectionRange(0, v.lastIndexOf('/'));
}
else if (element.createTextRange) {
var range = element.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', v.lastIndexOf('/'));
range.select();
}
}
}

Then call it from your input element like this:

<input type="text" id="date1" onfocus="setSelection(this);"/>

The method above assumes you use '/' as a date separator. You can
change it to '-' or whatever as neccessary. Best bet would be to
accomodate for both and check for which separator is used and
reference it. For example:

function setSelection(element) {

if (element.value.length 0) {
var v = element.value;
var separator = '/';
if (v.indexOf('-') 0) {
separator = '-';
}
if (element.setSelectionRange) {
element.setSelectionRange(0, v.lastIndexOf(separator));
}
else if (element.createTextRange) {
var range = element.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', v.lastIndexOf(separator));
range.select();
}
}
}

Or something like that...
Mar 1 '07 #5
On Mar 1, 9:12 am, "Simon" <simon1...@quicknet.nlwrote:
Thanks,

Still I find it very strange that you can't activate a key
byscript. Is this really impossible??
I can alert de keycode, when a key is pressed, but i can't
manually activate a keycode. Too bad.
<snip>

It would not do you any good if you could programmatically depress the
insert key anyway. You have no way of telling how may times the insert
key has been pressed before you pressed, so you do not know the
current sate (or the default) and so your key press is capable of
toggling the system into precisely the sate you don't want it in.

Richard.

Mar 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Larry Johnson | last post: by
reply views Thread by Brenton Garman | last post: by
3 posts views Thread by Me | last post: by
3 posts views Thread by jimmy | last post: by
reply views Thread by Me | last post: by
5 posts views Thread by =?Utf-8?B?Sm9obiBBdXN0aW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.