fri3drice
fri3drice

Reputation: 55

localstorage if and else not working

I'm trying to create a styleswitcher for my website. I've successfully created a dropdown menu and save it on the localstorage..Now, I'm trying to use the localstorage info to alert() via if and else but it isn't working. Here is the code:

JS

$(document).ready(function() {

    var item = window.localStorage.getItem('active_style');
    $('select[name=active_style]').val(item);

    $('select[name=active_style]').change(function() {
       window.localStorage.setItem('active_style', $(this).val());
    });

    if (item == "dark") {
        alert("WORKS");
    };

});

HTML

<select name="active_style" id="lol" class="dropdownselect" >
    <option value="">Default</option>   
    <option value="Dark" selected="selected">Dark</option>
</select>

I've no idea why it isnt working. Please help.

Upvotes: 0

Views: 81

Answers (2)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

Adding my own early comment as an answer to silence the complainers who feel this typo actually deserves an answer :)

"The value is case-sensitive, so you should have == "Dark" to match your <option value="Dark""

Upvotes: 1

vaso123
vaso123

Reputation: 12391

This is happens, because you are stored Dark and you compare it with dark. See the case-sensitive thing. The first D is uppercase.

Upvotes: 2

Related Questions