0

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?

2 Answers 2

3

in jquery

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, you won't mind a little upvote then, trying to get some rep around here :-)
@Alex that was an old answer, these days I'd use .on instead of .bind
1

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/

3 Comments

Out of curiosity, what is the advantage to using .on vs just $('#hospitalDropDown').change() ?
.change(), which is a shortcut for .bind('change') only attaches to currently loaded DOM elements. From the documentation: "The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live()."
thank you for taking time to answer my question, I used the first answer though.

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.