M_kul
M_kul

Reputation: 173

.onkeypress not working in chrome and safari

I am using the script below on my site. There is a form on the site and the idea is if user presses "enter" within that form focus should be set to another box.

The script runs perfectly in IE and FF but does not work in safari or chrome. I was originally thinking that the 2 later browsers have a different ID for the enter key however adding the "alert(evt.keyCode);" does not produce an alert which means that it seems .onkeypress does not work in those 2 browsers. How do I fix this?

var WebPartElementID ="ctl00_ctl34_g_db6615a7_4c3b_4a14_9bbc_43ce9d63c24d_FormControl0";
var WebPartData = document.getElementById(WebPartElementID);    
WebPartData.onkeypress = stopRKey;    

function stopRKey(evt) {
            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
            alert(evt.keyCode);
            if ((evt.keyCode == 13)) { document.getElementById("ctl00_ctl34_g_db6615a7_4c3b_4a14_9bbc_43ce9d63c24d_FormControl0_V1_I1_R1_I1_T4").focus(); return false; }
}

Upvotes: 1

Views: 5668

Answers (1)

seahorsepip
seahorsepip

Reputation: 4819

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onkeypress

Notes: The keypress event should be raised when the user presses a key on the keyboard. However, not all browsers fire keypress events for certain keys.

Use keyup or keydown instead.

More info on differences: http://www.quirksmode.org/dom/events/keys.html

Seems some browsers don't see the enter key as inserting a character.

Upvotes: 1

Related Questions