Keep The Selected Value After Submit October 31, 2022 Post a Comment I have a time drop down selection and i want to keep the selected value after the submit button has been pressed. Html here Solution 1: If you have the jQuery Cookie Plugin, you can use it to store the value of the select into cookie every time a selection is made and whenever the form loads it would check if the cookie is set and use the value in the cookie. $(function() { var timeCookie = $.cookie( "timeCookie" ), selElem = $('select[name=pickupdatehour]'); selElem.on('change', function() { $.cookie( "timeCookie", this.value ); }); if( timeCookie != undefined ) { selElem.val( timeCookie ); } else { $.cookie( "timeCookie", selElem.val() ); } }); Copy HERE IS a working demo. Solution 2: As mentioned in the comment above unless you're storing the value in a database and pulling that out each time your best bet would be either a cookie or localstorage. Personally I've found localstorage to be easier to work with and unless you need IE7 support you should be fine to use that. http://caniuse.com/#search=localstorage You could try something like this (untested): // On submit var pickupdatehour = $('#pickupdatehour').val() localStorage.setItem('storedPickup', pickupdatehour); // On the pages that have the select box jQuery(document).ready(function () { var loadedPickup = JSON.parse(localStorage.getItem('storedPickup')); $('#pickupdatehour').val(loadedPickup); }); Copy Edit: Sorry missed that it was name not ID in your select box. Use the select[name=pickupdatehour] instead of the #pickupdatehour as a selector. Share Post a Comment for "Keep The Selected Value After Submit"
Post a Comment for "Keep The Selected Value After Submit"