Back

Technologies:

javascriptjavascript
avatar
Tolerim
a day ago

What is the problem with functions?

The given text includes a closed question on a website and a piece of JavaScript code. The code contains two functions related to a search bar and a back button from a project info page. The user is facing an issue where the back button function is not working due to the search bar function. They are asking for a solution to this problem.
Answers(1)
avatar
Tolerim
a day ago
Verified Answer
The issue is not related to JavaScript libraries. Looking at the code provided, the issue seems to be related to the functionality of the two click events on elements with class "search-bar" and "search-bar-footer" that affect the CSS property display of the element with class "search-page". This causes a conflict with the click event on the element with class "back", which is possibly located within the element with class "search-page". One possible solution is to add event listeners for the click events instead of directly assigning functions to them. This will allow for better control over the order in which the events are fired. For example:
let searchBar = document.querySelector(".search-bar");
let searchBarFooter = document.querySelector(".search-bar-footer");
let searchPage = document.querySelector(".search-page");
let cancleBtn = document.querySelector("#cancle-btn");

searchBar.addEventListener("click", function () {
    searchPage.style.display = "flex";
});
searchBarFooter.addEventListener("click", function () {
    searchPage.style.display = "flex";
});
cancleBtn.addEventListener("click", function () {
    searchPage.style.display = "none";
});

// Back Btn From Project Info 
let back = document.querySelector(".back");

back.addEventListener("click", function(){
    history.back();
});
;