craig
craig

Reputation: 581

dropdown and local storage issues

I have a drop down with several options. On page refresh i need the option that was previously selected to be selected again.

Here is my javascript.

A button press initiates saveQ1(id) function when the page loads it initiates loadQ1() function

//Save answer 1 to LocalStorage
        function saveQ1(id) {
        var answer = [document.getElementById("answer1").selectedIndex].value;
        localStorage.setItem('savequestion4656290', answer);
        }

         function loadQ1() {
        //if something was already saved....
        if (localStorage.getItem('savequestion4656290')) {
        var answer = localStorage.getItem('savequestion4656290');
        } else {
        var answer = ' ';
        }

//Put Answer 1 back in to box
        //I NEED SOMETHING HERE WHICH CHOOSES THE CORRECT OPTION (as remembered by local storage)       
         }

What should go in the end part to make the previously saved choice display. Im not even sure its saving properly. Thanks

Upvotes: 0

Views: 114

Answers (1)

BenM
BenM

Reputation: 4278

If I'm reading this correctly you need to add a line like

var answer1 = document.getElementByID("answer1");
answer1.selectedIndex = answer

Upvotes: 1

Related Questions