event.preventDefault() on first click then remove
You can pass false
to one():
$(".subnav a").one("click", false);
Passing false
instead of a handler is equivalent to passing a handler that returns false
, effectively stopping the event's propagation and preventing its default behavior.This is explained in the documentation for bind():
In jQuery 1.4.3 you can now pass in
false
in place of an event
handler. This will bind an event handler equivalent to:function() {
.
return false; }
How to reenable event.preventDefault?
You would have to unbind the event and either rebind to a separate event that does not preventDefault or just call the default event yourself later in the method after unbinding.
There is no magical event.cancelled=false;
As requested
$('form').submit( function(ev){
ev.preventDefault();
//later you decide you want to submit
$(this).unbind('submit').submit()
});
How to disable preventDefault in an event listener using Vanilla JS
I think you should return true/false
from your validateForm()
function to prevent/submit the form.
1. You have to return value from the function in the html markup:
<form id="contact_form" onsubmit="return validateForm();">
2. Return true/false
from your validateForm()
based on validation.function validateForm() {
var error = "";
................
................
................
if (error != "") {
document.getElementById("errorDiv").style.display = "block";
document.getElementById("errorDiv").innerHTML = "<b>There wer error(s) in the form, please complete all required fields</b>" + error;
return false;
} else {
return true;
}
}
Does not required to prevent form submit so remove this code.
var element = document.querySelector('form');
element.addEventListener('submit', event => {
event.preventDefault();
});
restore event to default action in jquery after event.prenventdefault
Hard to answer without better specifics, but if I understand what you have properly if you have something you can track state with you can preventDefault when you wish to block this behavior and let it fall through otherwise:
Psudocode:
var state = {
blockArrows: true;
}
$('#my_widget').keydown(function(e) {
if(state.blockArrows) {
e.preventDefault();
}
// ...else allow the event to do its normal thing....///
});
Related Topics
Get the Current Year in JavaScript
JavaScript String Array to Object
How to Access Nested JSON Data
Gmail Extension, Sendmessage to Background from Page Context
Rendering to Js with Jinja Produces Invalid Number Rather Than String
Quickest Way to Pass Data to a Popup Window I Created Using Window.Open()
D3.JSON Method Doesn't Return My Data Array
Attaching Click Event to a Jquery Object Not Yet Added to the Dom
What Is the Point of Void Operator in JavaScript
How to Chain These Functions Together with Promises
Javascript: Dynamically Creating Variables for Loops
How Do Print the Console Output of the Page in Puppeter as It Would Appear in the Browser
How to Write Regex to Validate Dates
Adding Csrftoken to Ajax Request
Why Does This Foreach Return Undefined When Using a Return Statement