Treehouse: Grow your CSS skills. Land your dream job.

Javascript – Remember what was selected after refresh

  • # May 27, 2011 at 10:47 am

    Hi all,

    I have a simple drop down that allows you to choose a country. On my site if the address in the form comes out wrong it refreshes the page and brings up a validation box. If someone choses Canada it defaults back to the US when the page refreshes. With javascript could it remember Canada after the refresh?

    # May 27, 2011 at 12:57 pm

    You could simply store some cookies, you could do javascript to accomplish this. Can’t really give you much more than that as I’m not proficient enough in JS, but ‘cookies’ are what you’re looking for.

    # May 27, 2011 at 3:10 pm

    Great! I’ll give this a try.

    # May 27, 2011 at 3:14 pm

    Yep, as JavaScript is client-side, the only thing you’ll be able to do is set cookies. It’s nice and easy, just set the cookie when they choose Canada and then read it on page load

    # May 29, 2011 at 11:08 am

    Or, you could just set the location hash:



    document.getElementById('country').addEventListener('change', function(){
    window.location.hash = this.value;
    }, false);
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.