Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Parent of DOM Object 1

Status
Not open for further replies.

maxhugen

Programmer
May 25, 2004
498
AU
I'm trying to capture and prevent the Backspace keypress from sending the Browser back, when a user is in a form, as some have lost the data they've entered after pressing backspace.

However, I need to allow it when I'm in a form field. The code I'm using so far is successfully preventing the Backspace, but in all circumstances:

Code:
function CancelBackspace(e) {
    var keyPressed = (e.which)?e.which:window.event.keyCode;
    return (keyPressed==8)?false:true;
}

<body onkeydown="return CancelBackspace(event)">

Is there some way that I could use target property of the event, and determine if the target's parent is a form?

MTIA

Max Hugen
Australia
 
You can do it like this.
[tt]
function CancelBackspace(e) {
var keyPressed = (e.which)?e.which:window.event.keyCode;
var b_form=(e.target)?(!!e.target.form):(!!window.event.srcElement.form);
return (keyPressed==8 && !b_form)?false:true;
}[/tt]
 
Thanks tsuji

That works fine. Just a question pls: what is the actual meaning of "!!" ? (seems like "not not"...)

Thanks, Max

Max Hugen
Australia
 
It just coerses the probing of existence of attributes or methods to boolean return leaving no alternative to the interpretation of the intension of the scripters. In some restricted situations, I like to make that semantic explicit to the comfort of scripters at the expense of cpu cycles. In some others, let the default behaviour of the script engine would suffice.
 
Thanks tsuji, its very elegant... now that I've been thinking on it more, I can see that the first "!" forces a boolean result, and the second "!" reverses the boolean to the output we'd like. Cool.

Max Hugen
Australia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top