bravo14 Posted March 21, 2022 Share Posted March 21, 2022 Hi all Using a from with this code <form class="search-form"> <input type="text" placeholder="Search..." id="txtSearch" autocomplete="off" value=""> <button id="btnSearch" class="search-btn"> <i class="flaticon-magnifying-glass"></i> </button> </form> and the jquery below <script type="text/javascript"> var q = $("#txtSearch").val(); $('#btnSearch').click(function(){ location.href = "/recipes/search/" + q; }); </script> I am trying to redirect to a page /recipes/search and then the inpit value from the search field, but the input value is not appending. I am hoping I am missing something simple? Quote Link to comment https://forums.phpfreaks.com/topic/314610-jquery-append-input-value-to-url/ Share on other sites More sharing options...
requinix Posted March 21, 2022 Share Posted March 21, 2022 You need the value of #txtSearch at the time the button is clicked, not when the page loads. And if you're going to be putting arbitrary values into the URL then you need to use a function like encodeURIComponent for it. Quote Link to comment https://forums.phpfreaks.com/topic/314610-jquery-append-input-value-to-url/#findComment-1594436 Share on other sites More sharing options...
Barand Posted March 21, 2022 Share Posted March 21, 2022 Also, you have a button element inside a form. That will cause the form to be submitted and your page to reload when the button is clicked. Remove the <form> tags, or change the button element to a span element styled to look like a button, or suppress the default action when the button is clicked Quote Link to comment https://forums.phpfreaks.com/topic/314610-jquery-append-input-value-to-url/#findComment-1594437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.