Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

2 drop downs using onchange, want to keep both selections

Posted on 2013-11-19
7
Medium Priority
?
323 Views
Last Modified: 2013-11-19
I have 2 drop downs that refresh the page when a selection is made and the id gets put in the URL.

I have them both working separately but I don't know how to put them together so that both selections will stay on the page refresh and both will be in the URL for use  at the same time

I'm using the onchange javascript functions below and each drop down is in a separate sub procedure.  Any advice on where to go from here would be appreciated thanks.

<script type="text/javascript">   
    function OnChange(dropdown, Product_id) 
    {
        var myindex = dropdown.selectedIndex
        var SelValue = dropdown.options[myindex].value;
        var baseURL = '/product.asp?color_id=' + SelValue + '&product_id=' + Product_id;
        top.location.href = baseURL;

        return true;

    }
    function OnChangeSize(dropdown, Product_id) {
        var myindex = dropdown.selectedIndex
        var SelValue = dropdown.options[myindex].value;
        var baseURL = '/product.asp?size_id=' + SelValue + '&product_id=' + Product_id;
        top.location.href = baseURL;

        return true;
    }
</script>

Open in new window

0
Comment
Question by:AdrienneSperber
  • 3
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
Big Monty earned 2000 total points
ID: 39659926
try something like this:

 <script type="text/javascript">   
    function OnChange(dropdown, Product_id) 
    {
        var myindex = dropdown.selectedIndex
        var SelValue = dropdown.options[myindex].value;
        var baseURL = '/product.asp?color_id=' + SelValue + '&product_id=' + Product_id;
 
        if( document.getElementById('otherDropDownName').selectedIndex )
            baseURL += '&size_id=' + document.getElementById('otherDropDownName').options[ document.getElementById('otherDropDownName').selectedIndex].value;

        top.location.href = baseURL;

        return true;

    }
    function OnChangeSize(dropdown, Product_id) {
        var myindex = dropdown.selectedIndex
        var SelValue = dropdown.options[myindex].value;
        var baseURL = '/product.asp?size_id=' + SelValue + '&product_id=' + Product_id;
 
        if( document.getElementById('otherDropDownName').selectedIndex )
            baseURL += '&color_id=' + document.getElementById('otherDropDownName').options[ document.getElementById('otherDropDownName').selectedIndex].value;

        top.location.href = baseURL;

        return true;
    }
</script>

Open in new window


you'll need to substitue your dropdown names to get this working
0
 
LVL 9

Author Comment

by:AdrienneSperber
ID: 39660114
Thanks, this is keeping the selections but only refreshing the page and updating the URL on the very first selection from either drop down.  Any idea?

Sorry very new to programming, my brain is mush from trying to do this!
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 39660117
do you have a page I can see?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 34

Expert Comment

by:Big Monty
ID: 39660161
sorry, change

if (document.getElementById('Color_id').selectedIndex)

to

if (document.getElementById('Color_id').selectedIndex > 0 )

and change

if (document.getElementById('Size_id').selectedIndex)

to

if (document.getElementById('Size_id').selectedIndex > 0 )
0
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39660401
I know this is not pure javascript, but I have always had good luck using chained http://www.appelsiini.net/projects/chained where you can have one drop down determine what the next is going to be and no page refresh.

You could also throw a querystring on the page refresh and use request.querystring to grab the data and set the selected value of the drop down.
0
 
LVL 9

Author Closing Comment

by:AdrienneSperber
ID: 39660635
Got it to work, thank you so much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question