1

How do I retain whatever was selected from the dropdown even after page refresh so user knows what he/she selected using jquery? or javascript?

<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')"> 
      <option disabled="disabled">Select Hospital</option> 
      <option value="http://mysite.com/events/Pages/default1.aspx">All Hospitals</option>
      <option value="http://mysite.com/events/Pages/default1.aspx?hos=Dyer">Dyer</option>
      <option value="http://mysite.com/events/Pages/default1.aspx?hos=Carmel">Carmel</option>
    </select>
Zoe
  • 27,060
  • 21
  • 118
  • 148
Anju Thapa
  • 257
  • 6
  • 17
  • 32
  • Postback? I'd use the server-side language of your choosing. – Ry- Jan 05 '12 at 02:55
  • 1
    There are a couple of ways to do this. The easiest is probably to use cookies. – glortho Jan 05 '12 at 02:55
  • No access to the server!! Has to be done in the client side using jquery or javascript – Anju Thapa Jan 05 '12 at 03:01
  • Well the answer will be almost the same as for your other question about doing the same thing with radio buttons: http://stackoverflow.com/q/8736869/615754 – nnnnnn Jan 05 '12 at 03:08

2 Answers2

1

Try this:

<select id="hospitalDropDown" onchange="window.open('http://mysite.com/events/Pages/default1.aspx?hos='+this.value,'_top')"> 
  <option disabled="disabled">Select Hospital</option> 
  <option value="All">All Hospitals</option>
  <option value="Dyer">Dyer</option>
  <option value="Carmel">Carmel</option>
</select>

$(document).ready(function(){
    var value = window.location.href.match(/[?&]hos=([^&#]+)/) || [];
    $('#hospitalDropDown').val(value[1]);
});
JT...
  • 195
  • 1
  • 11
  • 1
    If you end up needing a more robust method of hitting the querystring (e.g., multiple times per request), it's worth checking out the [question on accessing the querystring in JavaScript](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) or using [URI.js](http://medialize.github.com/URI.js/) against `window.location.href`. – patridge Jan 05 '12 at 18:49
0
<select id="hospitalDropDown"> 
    <option value="">All Hospitals</option>
    <option value="Dyer">Dyer</option>
    <option value="Carmel">Carmel</option>
</select>
<script type="text/javascript">

$(document).ready(function() {
    $('#hospitalDropDown').val('<?php echo $_GET['hos']; ?>');
    $('#hospitalDropDown').change(function() {
        location.href = 'http://mysite.com/events/Pages/default1.aspx?hos=' + $(this).val();
    });
});
</script>
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • At no point did they mention having access to PHP. In fact, they commented they have no access to server-side code. This answer was even more incorrect on the [duplicate where you posted this exact answer first](http://stackoverflow.com/a/8743784/48700), where they explicitly stated such a requirement in the question itself. – patridge Jan 05 '12 at 14:43