Jump to content

Cancelling an event?


Go to solution Solved by ginerjm,

Recommended Posts

Ok - I have an input field that makes a call to a js function at onchange. The function returns true/false and the calling field is ready for that. Problem is the function tries to set the focus back to the input field it just edited before returning but the system continues to move focus to the next field since I pressed tab to trigger the onchange event.

 

How does one "cancel" the tab event in that onchange handler so that the user has to fix his input before moving on?

Link to comment
https://forums.phpfreaks.com/topic/301038-cancelling-an-event/
Share on other sites

Using just vanilla Javascript? It's passed to the function as the first argument.

element.addEventListener("change", function(e) {
	e.preventDefault();
	// ...
});
Unless you want to be cross-browser compatible, in which case don't use vanilla Javascript.
Link to comment
https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532191
Share on other sites

Don't mess with core features of the browser. Many people rely heavily on tab navigation, especially when they're visually impaired. If your JavaScript silently swallows the key press, that's a problem. It's also the user's decision when to fill out which field. Simply mark the input as invalid and let them decide when they fix it.

Link to comment
https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532192
Share on other sites

I understand that the field is required and must be validated, but deleting(!) the user input and not letting the user out of the field until they've gotten it right borders on violence. ;)

 

I've never seen a form do anything like this, not even when the data is very critical. Yes, forms may validate the input back and forth, but they don't punish the user for filling out the fields in the “wrong” order. As long as they don't submit the form, users are free to do whatever they want.

 

If you insist on breaking this contract, you'll have to rely on hacks (like watching every keystroke and suppressing tabs).

Link to comment
https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532195
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.