Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to retain the selected value in the dropdown after page refresh ?

    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 54.8k
    Comment on it

    To retain the selected value in the dropdown on refresh, sessionStorage is used to store the value within the user's browser. First, the values have to be set using sessionStorage.setItem("SelItem", selVal); SelItem is a variable in which we are setting the value of selVal . Now, we can get the value using sessionStorage.getItem("SelItem") after page refresh.

    Example:

    index.html

    <select id="sort-item">
                 <option value="Alphabetical" data-sort="1">Alphabetical</option>
                 <option value="low to high" data-sort="2">Low to High Price</option>
                 <option value="high to low" data-sort="3">High to Low Price</option>
    
     </select>
    

    abc.js

    window.onload = function() {
        var selItem = sessionStorage.getItem("SelItem");  
        $('#sort-item').val(selItem);
        }
    
        $('#sort-item').change(function() { 
            var selVal = $(this).val();
            sessionStorage.setItem("SelItem", selVal);
        });
    

 3 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: