Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

How to submit form while event.preventDefault() is applied

I have a page that contains a form for logout that is sending POST request to an endpoint and a form for live search from the database through AJAX call. On document load the page is running the function for showing results from the database which has event.preventDefault() in it which is stopping the form submission for the whole page. The issue is that I can't send the logout form now (because event.preventDefault() is applied). This is how my logout form looks

<a href="javascript:void(0);" onclick="event.preventDefault();$(this).children('form').submit();">
    <form method="POST" action="{{ route('logout') }}">
        @csrf
        <span>@lang('main.navigation_logout')</span>
    </form>
</a>

This logout form is not working only on the page with the live search (Because of the event.preventDefault() in the live search function). How can I allow this logout form submission?

0 likes
7 replies
tykus's avatar

the page is running the function for showing results from the database which has event.preventDefault() in it

What does this look like?

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

Why not just target the specific form(s) on the page and not just EVERY form !

1 like
lat4732's avatar
Level 12

It was literally working with targeting the form that I want to disallow submission but I made some changes and now It's not working. I have the following error in the console:

Uncaught TypeError: $(...).preventDefault is not a function

on this line

$(document).on("submit", "#companies-filters", function(){
        $("#companies-filters").preventDefault(); // THIS LINE
});

I have no idea what happened. Any idea what's wrong?

tykus's avatar

@Laralex you preventDefault on the event, not on the node itself:

$(document).on("submit", "#companies-filters", function(event){
    event.preventDefault();
});
1 like

Please or to participate in this conversation.