So here is my issue.
I’m developing with WordPress and using a plugin to manage a hotel booking process.
The form is generated and has no classes/ids on so I’m having to locate it via jQuery.
jQuery(document).on('ready', function(){
// Clicking on submit button
jQuery('.booking-buttons input[type="submit"]').on('click', function( evt ) {
if ( validation() ) {
console.log('yes');
return true;
} else {
console.log('no');
evt.preventDefault();
return false;
}
});
validation = function() {
// if fields validate
// return true
// else
// return false
// return false
}
});
I’ve also tried various onsubmit/click variations and the form still submits no matter what, the code has also been placed before and after the plugins JS includes and still no joy.
An interesting thing as well, it works in Chrome Canary but nothing else.
Has anyone else had an issue like this and have any guidance?
Thanks
You could simply use in your HTML:
Or if you really want to use jQuery:
check but i am not rewrite your code .. i used it from my project that i give you as a example
Try attach to the form
submit
event instead:Based on your comments: As it is in a DOM ready handler the only other issue may be dynamic form creation.
In that case use a delegated event handler:
e,.g.
This has the advantage that it will work on dynamically created forms. Basically it catches all submit events bubbling up to the document, then applies the jQuery filter, then applies the function to any matching elements that caused the event.
If you allow for multiple forms on the page, then you need to pass the “current” form to the validation methods: