views:

1320

answers:

1

Hello, I have a simple html option/select (dropdown) menu. I want to use JQuery to redirect links when an option is selected. I have a "go" button in noscript tags in case javascript is disabled, but in the case that the user has javascript..I would like the redirection to happen automatically on-click. Could somebody please guide me on how to accomplish this using jquery (I have this done using simple javascript 'onclick' events but I'd like to move all of my code to jquery)?

Right now my code looks like this (the function gets call from the 'onclick' event):

function option(dropdown) {
        var myindex  = dropdown.selectedIndex
        var SelValue = dropdown.options[myindex].value
        var baseURL;
        if(SelValue=="1")
            baseURL="something1";
        else if(SelValue=="2")
            baseURL="something2";
        else if(SelValue=="3")
            baseURL="something3";
        top.location.href = baseURL;

        return true;
}
+2  A: 

To bind the click element in jQuery you can do

$('#elementId').click(function(){
  //do redirection
});

But for you case I think you need to bind the change event

$('#elementId').change(function(){
  var optionSelectedValue = $('#elementId option:selected').val();
  if(optionSelectValue == value1) {
     newUrl = url1;
  }
  else if(optionSelectValue == value2) {
     newUrl = url2;
  }
  top.location.href = newUrl;
});
Daniel Moura
Thanks! that was simple enough
es11
any chance you know the code to perform the redirection off hand?
es11
What exactly does redirect links mean?
Michael
when the option in the drop down is selected, I need the page to get redirected (i.e. a hyperlink to a new page)
es11