0

Currently I have a menu of a few select drop-downs. I've made it so whatever option is selected in the middle drop down, it's displayed as the title/label for the bottom drop down. This is a div I'm displaying over multiple html pages. Every time I change the middle option, the title in the bottom does successfully change to what I want but once it loads a page, the bottom title/label disappears and goes back to the default blank. How do I make it so that title/label is what it should be even after page load?

HTML:

<div class="form-group">
    <label class="col-md-4 control-label">Section:</label>
    <div class="col-md-8">
        <select id="middle" class="form-control">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
        </select>
    </div>
</div>

<br /><br />

<div class="form-group">
    <label class="col-md-4 control-label" id="output"></label>
    <div class="col-md-8">
    <select id="bottom" class="form-control">
        <option>11</option>
        <option>12</option>
        <option>13</option>
    </select>
    </div>
</div>

jQuery:

$('#middle').change(function() {
    $('#output').text($(this).find(":selected").text());
});
Z. Dmitry
  • 321
  • 4
  • 22
J. Doe43
  • 207
  • 2
  • 6
  • 20
  • Is anything selected by default on page load from `select#middle` dropdown? – Alexander Staroselsky Jun 27 '17 at 17:15
  • Not currently, no. – J. Doe43 Jun 27 '17 at 17:16
  • 1
    The `$('#output').text($(this).find(":selected").text());` selector is looking for an option to be selected on page load. As you have no default option selected on page load, the jQuery selector is coming back empty and therefore no text from the `.text()` method. You could consider a default selected option following this answers in this [question](https://stackoverflow.com/questions/3518002/how-can-i-set-the-default-value-for-an-html-select-element) – Alexander Staroselsky Jun 27 '17 at 17:28

2 Answers2

1

Jquery does not save data or cache data after refreshing the page. Thats why you are not getting the values.

To recover it you need to use Html5 $cookie. Please do google it about its uses.

Ranit
  • 47
  • 8
  • 2
    You can also use JavaScript's [`localStorage`](https://www.w3schools.com/html/html5_webstorage.asp), no need for cookies – myfunkyside Jun 27 '17 at 17:25
1

If you just want the title of #bottom to say 1 on every pageload, same as the #middle select is saying, you can use this:

$('#middle').change(function() {
  $('#output').text($(this).find(":selected") ? $(this).find(":selected").text() : $(this).find(":first").text());
}).change(); //trigger change on pageload

$('#middle').change(function() {
  $('#output').text($(this).find(":selected") ? $(this).find(":selected").text() : $(this).find(":first").text());
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="form-group">
  <label class="col-md-4 control-label">Section:</label>
  <div class="col-md-8">
    <select id="middle" class="form-control">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
      <option>9</option>
      <option>10</option>
    </select>
  </div>
</div>

<br /><br />

<div class="form-group">
  <label class="col-md-4 control-label" id="output"></label>
  <div class="col-md-8">
    <select id="bottom" class="form-control">
      <option>11</option>
      <option>12</option>
      <option>13</option>
    </select>
  </div>
</div>
jsfiddle: https://jsfiddle.net/hgep8hcg/
  • This code checks if there is a selected option, and if so, uses its value. If there is no selected option, it uses the first option's value.
    This code, inside .text(), may look a little weird. It's called a ternary operator, click the link if you want to understand it.

IF YOU WANT TO STORE THE SELECTED VALUES, READ ON:

Your easiest option would be localStorage, provided that browser support is sufficient for your goal.
(I use sessionStorage here, just so everybody's localStorage doesn't get bloated with test values.)

This would do the trick:

$("#middle").change(function() {
  var value = $(this).find(":selected").text();
  $("#output").text(value);
  sessionStorage.setItem(this.id, value);
}).val(function(){return (sessionStorage.getItem(this.id) ? sessionStorage.getItem(this.id) : $(this).find("option:first").text());}).change(); //set values on pageload

jsfiddle: https://jsfiddle.net/k3htre4d/2/
codepen: https://codepen.io/anon/pen/OgOdLZ?editors=1010

  • In your change handler, I set the localStorage, using the select's ID as the key, and the select's text-value as value.
  • After the change handler, I set the select's value, using the localStorage key that matches the select's ID, if that key exists. If it doesn't exist, I use the select's first option's text.
    That's this line: return (... ? ... : ...);. It's called a ternary operator, click the link if you want to understand it.

MORE EFFICIENT MIGHT BE:

$("#middle").change(function() {
  $("#output").text($(this).find(":selected").text());
});

$(".form-control").change(function() {
  sessionStorage.setItem(this.id, $(this).find(":selected").text()); //store select's value on change
}).each(function() {
  if (sessionStorage.getItem(this.id)) {$(this).val(sessionStorage.getItem(this.id));} //set select's value on pageload
}).change(); //trigger change on pageload

jsfiddle: https://jsfiddle.net/k3htre4d/1/
codepen: https://codepen.io/anon/pen/dRZayW?editors=1010

  • In this code the values of all the selects with class form-control are stored into localStorage on change. All the selects get their own key in localStorage based on their ID. So now you don't have to duplicate the handler for every single select. Just make sure every select you wish to store the value for, has class form-control (or whatever class you choose).
    Using this principle, you could also add an extra class to only those selects that you wish to store the values for, and than use that class for the handler instead of .form-control.
  • The code that is only used by the #middle select, is in a separate handler.
myfunkyside
  • 3,890
  • 1
  • 17
  • 32