I'm trying to trap the submit button click event and scroll the page to the top if it's not already there.
For some reason the event doesn't trigger first time, i.e the sliding to the top of page if the scrolling is greater than 0, doesn't happen. It only activates the second time around.
var buttonSearch = function () {
jQuery(document).ready(function () {
$("#DashboardSearchButton").submit(function () {
if (window.scrollY != 0) {
window.scrollTo({ top: 0, behavior: 'smooth' });
}
});
});
};
<input id="DashboardSearchButton" type="submit" value="Search" onsubmit="buttonSearch();" />
Any ideas or suggestions? Am I missing something? Thank you.
buttonSearch()creates a new event handler for the search button? ie only when you click the button is the click button event handler created. Remove theonsubmit=and thevar buttonSearch=function(){(and}) lines and you're good to go.