11

I have a select box with 10 options. Each time you select an option it sets in the localstorage the value you selected with id "select1".

For example: If you select the first option you get in the localstorage: Key: Emailclient - Value: Option1.

Now, Iom trying to make the value of the localstorage, the selected attribute in the select form.

This is what I tried but doesn't seem to work:

if (localStorage.getItem("Select1") == "Option1"){ 
    $('#selectbox').val("Option1").attr("selected");
}

What I'm I doing wrong?

EDIT:

This is my code:

<select id="selectbox" >
                        <option>Default</option>
                        <option>Option 1</option>
                        <option>Option 3</option>
                        <option>Option 3</option>
                        <option>Option 4</option>
                        <option>Option 5</option>
                        <option>Option 6</option>
        </select>

Thanks

5 Answers 5

31

This works:

var optionValue  = localStorage.getItem("Select1")
$("#selectbox").val(optionValue)
.find("option[value=" + optionValue +"]").attr('selected', true);
Sign up to request clarification or add additional context in comments.

1 Comment

Friend, you save me (y).
1

I think you might be looking for something like this. http://jsfiddle.net/tnPU8/3/

It loops through the options of the select box and compares their values with the values of b. If they are equal the index of the select box is changed so that the option that contains the value of b is displayed.

var b; //set equal to what you want to compare
$('#selectbox').find('option').each(function(i,e){
    console.log($(e).val());
    if($(e).val() == b){
        $('#selectbox').prop('selectedIndex',i);
    }
});

Edit: Using localStorage http://jsfiddle.net/tnPU8/7/

2 Comments

It doesn't work If im trying to retrieve from the localstorage. Here is an example: jsfiddle.net/uY7ck
Thank you sir. I found my way to work better because of less code. Your way, I will need to type the If statement as many options I have. My way, is just 3 line of code. Thanks alot though
1

Try this:

if (localStorage.getItem("Select1") == "Option1"){ 
    $('#selectbox option:contains(Option1)').prop('selected',true); // jquery 1.6+
}

if you are using a version of jQuery older than 1.6, replace .prop with .attr

Edit: more dynamic version

if (localStorage.getItem("Select1")) {
  $('#selectbox option:contains(' + localStorage.getItem("Select1") + ')').prop('selected',true);
}

1 Comment

it works for me, jsfiddle.net/uY7ck/2 check your id's. you were selecing a select with an id of emailclient when the only select on the fiddle had an id of selectbox
0

On further review, it looks as though you wanted something more fully feature link

search = "Option 5";
$('#selectbox option:contains('+search+')').prop('selected',true);

4 Comments

It's not logical to work and it doesn't. You're setting the selectbox option to selected of what?
@Korvin you don't even need to navigate to the selected option. just call .val() on the select for the same result.
@Tentonaxe his original question was asking for the value of the selected option, not the contents of the selected option, otherwise you're completely correct.
@jQuerybeast your jsfiddle will not produce your desired outcome because you are referring to the wrong select box 0.0
0

This is all you should need:

$("#selectbox").val(localStorage.getItem("selectbox"));

I've updated a previous fiddle for an example: http://jsfiddle.net/uY7ck/1/

EDIT: In the code you posted originally, assuming you are setting localStorage with the same select box, you would be comparing "Option1" (no space) with "Option 1" (space). That comparison will never work, so the option would never be selected.

1 Comment

On your edit: True. I found that to be a problem but anyways, all my options have no spaces. I found my way to work much better since I don't need to type the If statement as many times as my options, rather than only the 3 line code. Thanks alot though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.