ginerjm Posted March 18, 2016 Share Posted March 18, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/ Share on other sites More sharing options...
Solution ginerjm Posted March 18, 2016 Author Solution Share Posted March 18, 2016 Ok - never mind. Made a change to use onBlur instead of onChange and it works better than expected! Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532184 Share on other sites More sharing options...
requinix Posted March 18, 2016 Share Posted March 18, 2016 To answer the original question, use event.preventDefault(). Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532185 Share on other sites More sharing options...
ginerjm Posted March 18, 2016 Author Share Posted March 18, 2016 How do I get the current event to do the prevent call? Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532188 Share on other sites More sharing options...
requinix Posted March 18, 2016 Share Posted March 18, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532191 Share on other sites More sharing options...
Jacques1 Posted March 18, 2016 Share Posted March 18, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532192 Share on other sites More sharing options...
ginerjm Posted March 18, 2016 Author Share Posted March 18, 2016 I am trying to pre-edit the date field before submitting the form. I erase the bad input in my check function but I want the focus to remain on that field until a valid date is entered since it is required. Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532193 Share on other sites More sharing options...
Jacques1 Posted March 18, 2016 Share Posted March 18, 2016 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). Quote Link to comment https://forums.phpfreaks.com/topic/301038-cancelling-an-event/#findComment-1532195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.