1

I am trying to check if there is hos paramaeter in the url has anything and if there is, then pass that value as the selected attribute to the dropdown on page refresh, so the dropdown option remains selected even after refresh

   var value = window.location.href.match(/[?&]hos=([^&#]+)/) || [];

        if (value.length == 2) {
            $('#hospitalDropDown[value="' + value[1] + '"]').attr('selected', 'selected');

    }

Here is the dropdown:

<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')">          <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> 
Anjana Sharma
  • 4,535
  • 5
  • 37
  • 51

1 Answers1

0

It looks like your options have the querystring value—All Hospitals, Dyer, Carmel—as the text, but the whole url as the value.

As a result, match on your option's value with *=

if (value.length == 2) {
    $('#hospitalDropDown option[value*="' + value[1] + '"]').attr("selected", "selected");
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393