2

I have a simple select box with years and values like so

<div id="year_selection_area" class="controls text-center">
  <select class="form-control " name="Legislation year" id="dueYear">
    <OPTION VALUE='2017' >2017</OPTION>
    <OPTION VALUE='2016' >2016</OPTION>
    <OPTION VALUE='2015'>2015</OPTION>
    <OPTION VALUE='2014' >2014</OPTION>
    <OPTION VALUE='2013'>2013</OPTION>
    <OPTION VALUE='2012' >2012</OPTION>
    <OPTION VALUE='2011'>2011</OPTION>
    <OPTION VALUE='2010'>2010</OPTION>
    <OPTION VALUE='' DISABLED>MORE YEARS ADDED SOON</OPTION>
  </select>
</div>

i also am assigning selected at the beginning through a mix of jQuery and rails

<% if session[:year_browsing]==nil%>
  <% session[:year_browsing] = 2016%>
<% else %>
  <%= session[:year_browsing] %>
<%end%>
<script  type="text/javascript">
  jQuery(document).ready(function($) {
    $('.form-control option[value=<%= session[:year_browsing] %>]').attr('selected','selected');
  });

however I need to change session variable depending on what user clicks I tried an if/else statement but it executes all of the ruby as soon as it gets into that on change function

$( ".form-control" ).change(function() {
  alert( $(".form-control").val());
  var selectedValue = $(".form-control").val();

  if ( selectedValue=='2017'){
    <% session[:year_browsing] = 2017 %>
  }
  else if ( selectedValue=='2016'){
    <% session[:year_browsing] = 2016 %>
  }
  else if ( $(".form-control").val()=='2015'){
    <% session[:year_browsing] = 2015 %>
  }
  else if ( $(".form-control").val()=='2014'){
    <% session[:year_browsing] = 2014 %>
  }
  else if ( $(".form-control").val()=='2013'){
    <% session[:year_browsing] = 2013 %>
  }
  else if ( $(".form-control").val()=='2012'){
    <% session[:year_browsing] = 2012 %>
  }
  else if ( $(".form-control").val()=='2011'){
    <% session[:year_browsing] = 2011 %>
  }
  else if ( $(".form-control").val()=='2010'){
    <% session[:year_browsing] = 2010 %>
  }
  else{
    <% session[:year_browsing] = 0 %>
  }
});
3
  • 1
    you have to send a ajax method when you want to change session variable, you can not update session on views Commented May 25, 2016 at 11:36
  • I would suggest using browser local storage instead of session storage in order to achieve the same. Commented May 25, 2016 at 12:19
  • @Sebin thank you i posted my solution bellow Commented May 25, 2016 at 13:51

1 Answer 1

4

Thank you very much to Sebin (https://stackoverflow.com/users/1684772/sebin)

I ended up choosing to store it in browser storage with this code

<script  type="text/javascript">
  jQuery(document).ready(function($) {
    var myVar = localStorage['myKey'] || '2016';
    $('.form-control option[value='+myVar+']').attr('selected','selected');
  });

  $( ".form-control" ).change(function() {
    alert( $(".form-control").val()) ;
    var selectedValue = $(".form-control").val();
    localStorage['myKey'] = selectedValue;
  });
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.