0

I am trying to grab whatever is selected when user selects from the option and pass it to the current URL as querystring. What am i doing wrong? It always grabs the first option

var selectedOption = $("#ctl00_PlaceHolderMain_ctl00_DropDownList1 option:selected").val();

            $("#ctl00_PlaceHolderMain_ctl00_DropDownList1").change(function(e) {
                  window.location.href = 'http://somesite/events/Pages/default1.aspx?cat=' + selectedOption
            });   

And this is the HTML I am working on.

<select name="ctl00$PlaceHolderMain$ctl00$DropDownList1" id="ctl00_PlaceHolderMain_ctl00_DropDownList1">
            <option value="Select Category">Select Category</option>
            <option value="All Categories">All Categories</option>
            <option value="Cancer">Cancer</option>
            <option value="Health Lecture">Health Lecture</option>
            <option value="Heart Health">Heart Health</option>
    </select>

2 Answers 2

2

Change your jQuery to:

$("#ctl00_PlaceHolderMain_ctl00_DropDownList1").change(function(e) {
window.location.href = 'http://somesite/events/Pages/default1.aspx?cat=' + $(this).val()
}); 
Sign up to request clarification or add additional context in comments.

8 Comments

This question concerns a trivial problem, and a simple solution. I think it better to offer advice in these situations, rather than "here's your code"; it encourages the OP to actually learn about what went wrong and how to fix it, partly so we don't end up with SO full of dupes.
The user asked what he did wrong. I simply provided the answer which he can view and learn from. It may have been trivial to you, but it was a problem for the user. Advice won't help someone learn as much as a workable answer, but thanks for your two cents.
I'm not saying your answer is bad in any way, in fact thank you for contributing. I personally think it's better to give an explanation of what went wrong, how to correct and in some cases some sample code. Perhaps you could add an explanation to your answer as to why your code works?
@j08691 thank you. worked like charm! 2JamWaffles: thanks for encouraging those who want to help out!
@j08691 how do I pass a different url if the selected option is 'All Categories'. For 'All Categories' the url is only 'somesite/events/Pages/default1.aspx'??
|
2

You need to get the value inside the change() function. Right now it's grabbing the selected function and triggering the redirect after the change, so it doesn't detect the change in selection.

Example.

Comments

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.