Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect automatically when selecting an item from a select drop-down list

This page does it the way I want it to: http://www.web-source.net/javascript_redirect_box.htm

But I want to find a better way using jQuery, can anyone point me to a good script that uses jQuery to accomplish this?

like image 854
Brad Avatar asked Feb 24 '09 02:02

Brad


People also ask

How to redirect from drop down list in html?

This is done by using onChange event of dropdown listbox. Once the user selection one option then by onChange event we run one function SelectRedirect(). The function will receive the user selected option value like this. Here we have used getElementById to get the value of the selected option.

How to redirect to another jsp page based on option selected?

The page redirect is used to move the redirect response to another resource of the web page. Basically we can call the redirect pages using sendRedirect() method in the jsp on client-side request can be used within the server or outside of the server.


2 Answers

You don't need a pre-packaged script for this, just a couple lines of code.

// get your select element and listen for a change event on it
$('#selectEl').change(function() {
  // set the window's location property to the value of the option the user has selected
  window.location = $(this).val();
});
like image 94
FriendOfFuture Avatar answered Sep 18 '22 19:09

FriendOfFuture


Others have given good answers on how to do this, but...

Just a warning, IE handles select list onChange very differently from other browsers when someone is navigating via a keyboard. It counts every key up/key down press as an onChange event, meaning that you can't navigate a redirect select list via keyboard, killing accessibility for users who can't use the mouse. (Other browsers wait for an "enter" event, or a tab out of the select list, before firing onChange.) I've spent a long, long time trying to hack a workaround for this, and never fully solved the problem.

I don't know if jQuery accounts for this; hopefully it does, for your site's accessibility's sake.

It may be worth making the select list choose where to go, then have a button next to it actually activate the redirect. (Unless you don't care about IE users' accessibility.)

like image 38
Dan Lew Avatar answered Sep 17 '22 19:09

Dan Lew