Is there an opposite function of preventDefault() in JavaScript?
I think the problem you are having is that you are not looking for the delete key. Preventdefault does not cancel the event handler all together. But with your code once you hit the maximum length of your field the user will no longer be able delete any characters because the delete keypress is being cancelled.
The reason it works in IE is because IE does not fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. In Safari the keycode for delete is incorrect in the keypress event.
For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.
Second, look at the keycode and if it is delete or backspace then don't preventDefault.
if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
return false;
} else {
return true;
}
Opposite of e.preventDefault and e.stopPropagation();
Fixed by unbinding the event.
Focus
$('.form-control').focus(function() {
$('#element').on('scroll touchmove mousewheel', function(e){
e.preventDefault();
e.stopPropagation();
})
});
Focus out
$('.form-control').focusout(function() {
$('#element').unbind();
});
Undo preventDefault() method
You can't undo the e.preventDefault()
as you are dealing with a later instance of e
by the time you submit.
return false
does the same as e.preventDefault()
and e.stopPropagation()
, so you can simplify your code a little though by simply returning your flag.
var $form = $('form');
var canSubmit = false;
$form.data('working', false);
$form.submit(function(e){
if (form.validations.form($form)) {
if (!$form.data('working')) {
$form.data('working', true);
if (!canSubmit) {
e.preventDefault();
$.get(BASE+'file.txt', function(response){
canSubmit = true;
$form.submit(); // trigger recursive call
});
}
}
} else {
$form.data('working', false);
}
return canSubmit;
});
event.preventDefault() vs. return false
return false
from within a jQuery event handler is effectively the same as calling both e.preventDefault
and e.stopPropagation
on the passed jQuery.Event object.
e.preventDefault()
will prevent the default event from occuring, e.stopPropagation()
will prevent the event from bubbling up and return false
will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false
does not stop the event from bubbling up.
Source: John Resig
Any benefit to using event.preventDefault() over "return false" to cancel out an href click?
Opposite of PreventDefault() : Continuing ActionLink logic
You can trigger the event again:
$('#NameOfButton').bind('click', function (e, skip) {
if (skip) return; // check param
e.preventDefault(); // Stop click event
//Gather Data
var data = $(this).parents('form').first().serialize();
var el = $(this);
//Check Data , if saved to DB continue Click functionality
$.ajax({
url: '<%:Url.Action("Get")%>',
cache: false,
type: 'POST',
data: data,
success: function (result) {
if (result.Success == true) {
console.log('true');
el.trigger('click', [true]); // trigger same event with additional param
} else {
//do nothing
console.log('false');
}
}
});
});
What's the difference between event.stopPropagation and event.preventDefault?
stopPropagation
prevents further propagation of the current event in the capturing and bubbling phases.
preventDefault
prevents the default action the browser makes on that event.
Examples
preventDefault
$("#but").click(function (event) {
event.preventDefault()
})
$("#foo").click(function () {
alert("parent click event fired!")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo">
<button id="but">button</button>
</div>
Related Topics
Create a Newline for Every X Number of Characters
Replace Unicode Matches in JavaScript
How to Display JavaScript Datetime in 12 Hour Am/Pm Format
Weird Behaviour With on Click Event Binding; Event Not Firing
How to Set the Multi Sliders in One Page
Onclick Event Function in JavaScript
How to Hide the Android Keyboard Using JavaScript
How to Insert Space Every 4 Characters for Iban Registering
Render a Table Body in React Using the Map Function
Read Qrcode from a Web Page With Camera.
To Check If a String Is Alphanumeric in JavaScript
Angular:How to Add/Remove Files in the Angular
Document.Queryselector Always Returns Null
How to Translate the Following Jquery Code to React
Display a Loading Icon While Images Loads