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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to temporarily disable this script? 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hello,

I'm using the function below in a JS file in order to prevent users from accidentally submitting a form on enter key hit.

But I would like to disable this script on one form only (login form), without altering the JS file, and keeping in mind that there might be other forms displayed on the page that still need the script.

Does anyone have an idea on how to do that?

Code:
var nav = window.Event ? true : false;

    if (nav) {
    
    window.captureEvents(Event.KEYDOWN);
    window.onkeydown = NetscapeEventHandler_KeyDown;
    
    } else {
    
    document.onkeydown = MicrosoftEventHandler_KeyDown;
    
    }

function NetscapeEventHandler_KeyDown(e) {

  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false };
  
  return true;

}

function MicrosoftEventHandler_KeyDown() {

  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')

return false;
return true;

}

Thanks :)

 
what does your login form look like? Is it one/two/three fields? Are they all text fields?

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Hi vicvirk,

The login form contains :

- two text inputs (log + pass)
- one hidden input (form name)
- one submit input

That's all! :)
 
You may need to create another function to over-ride the previous one...

try this:

Code:
<form action="#" method="get" id="myFrm">
Name: <input type="text" name="tbx" onKeyDown="checkForEnter(event);" /><br/>
Password: <input type="password" name="pass" onKeyDown="checkForEnter(event);" /><br />
<input type="hidden" value="myFrm" />
<input type="submit" value="Login" />
</form>

<script language="javascript">
function checkForEnter(e) {
	var evt = window.event ? event : e;
	var intAction = evt.charCode ? evt.charCode : evt.keyCode;
	if (intAction == 13) 
		document.getElementById("myFrm").submit();
	} 
</script>

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top