Anju Thapa
Anju Thapa

Reputation: 257

Passing dropdown selected value as query string to URL

On select event of a HTML dropdown value, I want to trigger being directed to a certain URL and that selected value being appended to the URL as query string. How to achieve that in javascript or jquery?

<select id="hospitalDropDown">
            <option>All Hospitals</option>
            <option>Dyer</option>
        <option>Carmel</option>
        <option>Indianapolis</option>
        <option>Beech Grove</option>
</select> 

So if Dyer is selected, the result should be being directed to http://mysite.com/events.aspx?hosp=Dyer

Can someone help?

Upvotes: 0

Views: 5970

Answers (2)

gotofritz
gotofritz

Reputation: 3381

in jquery

$( '#hospitalDropDown' ).on( 'change', function( e ){
  document.location.href = "http://blah" + $( this ).val();
});

EDIT: changed "bind" to the more modern "on"

Upvotes: 3

nachito
nachito

Reputation: 7035

If you're using the latest version of jQuery, you can attach an event handler using the on() function.

$('#hospitalDropDown').on('change', function () {
    window.location.assign('http://mysite.com/events.aspx?hosp=' + $(this).val());
});

To see this in action: http://jsfiddle.net/73PEg/

Upvotes: 1

Related Questions