-1

I have an external list that is being read to form the values in the select list I have on my webpage. When an item is selected it updates 3 bar graphs that are located in another area of code. I use setItem() under the on(change) function to locally save the selectedIndex value of the list. Then in the top part of the code I use getItem() to get this same index value and set it equal to the variable id. I have my page automatically refreshing every 30 seconds. I want to take the value of "idx" and have it automatically select that same index when the page reloads. So that it reloads with the last selected index, instead of resetting to being unselected.

.script
    window.onload = function() {
      var idx = localStorage.getItem(document.title.toLowerCase() + '-selectedIndex');
      console.log('last selected index : ' + idx);
    //*****what would I put here that uses the value idx to have the same index automatically selected when the page reloads
};


$('#foo').on('change', function(context) {


  var bar1 = $('#progressbar1');
  var bar2 = $('#progressbar2');
  var bar3 = $('#progressbar3');


  localStorage.setItem(document.title.toLowerCase() + '-selectedIndex', this.selectedIndex);

  bar1.val(globalSheetResults1[this.selectedIndex]);
  bar2.val(globalSheetResults2[this.selectedIndex]);
  bar3.val(globalSheetResults3[this.selectedIndex]);
Adam Azad
  • 10,675
  • 5
  • 25
  • 63
Nicole Pinto
  • 344
  • 1
  • 17

2 Answers2

1

You just need to set the selectedIndex of your option element on the page load, and then fire the change event. So, something like:

document.getElementById('foo').selectedIndex = idx;
$('#foo').change();
BurningLights
  • 2,327
  • 1
  • 13
  • 22
0

A possible solution could be using cookies so that you can check whether a value was selected and so generate your graph.

See the plugin:

https://github.com/carhartl/jquery-cookie

You can then do:

$.cookie("foo", 'bar');

To delete:

$.removeCookie("test");

So you can check whether cookie exists onload and set that on change.

You can see a full explanation about that plugin here

Community
  • 1
  • 1
Cristiano Mendonça
  • 1,170
  • 1
  • 9
  • 21