I do have a header with a navigation bar. The navigation includes a search button that each time is clicked should change the word from "Search" to "Close Search" and open the search bar initially not displayed below the navigation bar. I tried different ways with .html, .text but the text in the search button sometimes does work and others doesn't. I tried different approaches, but it is not consistent. Every time I click on the "Search" button, it should change the text and open and close the search bar, not just once. I tried using toggle() as well. I paste here below the code for button and search bar.
Also, the search button should display a search icon, defined by the class "btn-search-icon", which disappears once the searchbar closes and the button has the text "close search". The search bar below has a "search" field, submit button and a cancel button. The cancel button should remove the value in the search field. This step works but it reloads the page and as a consequence it closes the search bar, while it should stay open.
Any ideas? I appreciate any help.
HTML:
<button class="bg-btn btn-search btn-search-icon" id="searchBtnBar" type="submit" value="true">Search</button>
div class="container search-bar-container">
<form id="bgSearchBar">
<input type="search" name="searchfield" value="Search" class="searchField">
<button class="bgsearchbtn">Search <i class="fa-solid fa-arrow-right"></i></button>
<button class="bgcancelhbtn"></button>
</form>
</div>
JQuery:
$('#bgSearchBar').hide();
$("#searchBtnBar").click(function () {
$(this).fadeOut(function () {
$(this).text(($(this).text() == 'Search') ? 'Close Search' : 'Search').fadeIn();
})
$(this).toggleClass('btn-search-icon');
$('#bgSearchBar').fadeIn();
})
$(".bgcancelhbtn").on("click", function(event) {
$(".searchField").val("");
});
I tried to make minimal changes so it would work.