Hello,

I am currently trying to figure out some event stuff with Javascript. I have the page capturing all onkeypress events by calling my function keyPress, and passing the event as an argument.

The key presses get passed to a text field that I have on the page (even if it's out of focus at the time of the key press) by using textfield.focus() within my keyPress function. This seems to pass the event along to the text field so that it registers the stroke and handles it as if the field was in focus at the time of the key press.

My problem lies in that I need to then grab the new value of the text field for use with another part of the script. It seems though that with the way I'm setting focus, it'll execute the rest of my keyPress function (with the outdated text field value) before the text field handles the event.

Is there a way to yield the event to this text field first?

Sorry, this was a long post, but I guess here's a short recap:
If I handle key presses via the body of the page, so that regardless of the text field's current state of focus it updates the text field accordingly, is there a way to have that happen first before the rest of my function that needs to use the new value of the text field?

Thanks in advance!

I don't think you can do it that way, initiating the keypress runs the code, and the keypress IS what would change the value of a textarea.
What I can recommend, though, is capturing the keypress key and appending that key's value to the text input.

What I can recommend, though, is capturing the keypress key and appending that key's value to the text input.

Thanks, that would be nice if it were that simple :P I'd have to handle the proper action if the key was not a regular, printable character, such as if the user pressed left arrow, backspace, delete, etc.

On top of that, there goes support for different keyboards, because of the placement of different keys in different places on international keyboards. Because the keycode would be run through my script instead of the native handling on the client's browser/OS.

I think I'll just find some alternate solution for this...

window.onload=(function(){
window.onkeypress = fillTextarea; });
function fillTextarea(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code).replace(/[^A-Za-z 0-9]/,'');
        var textarea = document.getElementById('text');
        if (textarea.focused){

        } else {
	if (character != ''){
             textarea.value += charater;
        } }
}
window.onload = function() {
  elem = document.getElementById('text');
     elem.focused = false;
     elem.hasFocus = function() {
         return this.focused;
     };
     elem.onfocus=function() {
         this.focused=true;
     };
     elem.onblur=function() {
         this.focused=false;
     };
 }

I can rewrite this for jquery, It'll be much cleaner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.