1

I made a dropdown with a html value change result, so after every click i get the new html value:

$("#list li a").on('click',function(){
 var val=$(this).html();
 $("#selector").html(val);
});

Simple enough to understand. What I'm trying to do is in the new reload, the html (val) stays on the new load. How can one do this?

I was thinking of HTML5: localStorage but really not sure if this is right?

localStorage.setItem(currentlist, $('#selector').val());

    window.onload = function() {
        var name = localStorage.getItem(currentlist);
    }

Not sure if Im doing this right or missing something. But this is where I'm at. Is this the right way to go...or do some cookie state?

9
  • 1
    what is currentlist ? is it a string? Commented Mar 24, 2016 at 14:51
  • 1
    Localstorage is definetly One way to Do this, but is it working or Do you have a specific problem? Commented Mar 24, 2016 at 14:51
  • Possible duplicate of How do I programatically set the value of a select box element using javascript? Commented Mar 24, 2016 at 14:55
  • The first parameter needs to be a string that doesn't change (I'm assuming you might be using an array), such as: localStorage.setItem("currentlist", $('#selector').val()). Commented Mar 24, 2016 at 14:56
  • 1
    @Cory This question is not a duplicate of the one you linked to. Commented Mar 24, 2016 at 14:57

1 Answer 1

2

JSFIDDLE DEMO

$("#list li a").on('click',function(){
    var val = $(this).html();
    $("#selector").html(val);
    localStorage.setItem("currentlist", val); //add to localStorage
});

and in your onload

var name = localStorage.getItem("currentlist"); //get from localStorage
$("#selector").html(name); //assign here
Sign up to request clarification or add additional context in comments.

9 Comments

what do you mean by: (name) would it not be (val)? Because its a multiple list.
(name) is just the variable you're actually assigning the value to, doesn't matter what you name it.
@Riskbreaker - I'm using the same variable name from the question in order to not confuse you. Ideally, you could use a better name and use the same for setting inside.
on a side note, don't use variable names like val, name, html etc. Be more specific like storedList or selectedHtml.
@Krishna great Let me try this method as far as (name), i guess I can use that or change it to (list) instead of (val)...thanks!
|

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.