I have an application using jQuery 3.2.1. It features 2 separate search forms called #primarySearch
and #advancedSearch
.
#primarySearch
is always visible, but #advancedSearch
is hidden on page load:
<form id="primarySearch">
</form>
<form id="advancedSearch" style="display: none;">
</form>
There is a button to show/hide #advancedSearch
like this:
/* Open Advanced Search */
$('#advancedSearchCta b').click(function(){
$('#advancedSearch').toggle();
$('#advancedSearchCta').hide();
});
/* Close Advanced Search */
$('#closeAdvancedSearch').click(function(){
$('#advancedSearch').toggle();
$('#advancedSearchCta').show();
});
This works fine.
Each form contains a number of inputs. I have implemented a button (#resetSearchBtn
) to clear all the form inputs - effectively a "Reset Search" button:
$('#resetSearchBtn').click(function(e){
e.preventDefault();
$(':input','#primarySearch, #advancedSearch')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
This works fine if the #advancedSearch
is visible. However if the user closes the advanced search - thus toggling it back to display: none;
, it does not work. Basically, it will only reset the search if #advancedSearch
is visible.
I can't rely on this because a user may open the advanced search, enter something, close it, and then try and do a reset. It will leave their data in #advancedSearch
. The application is ajax based and reacts to form changes. So it basically submits the "wrong" data to the server.
Is this normal behaviour or some conflict in my app I should be looking for? What are the solutions to this?