Solved

2 drop downs using onchange, want to keep both selections

Posted on 2013-11-19
7
307 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 32

Accepted Solution

by:
Big Monty earned 500 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 32

Expert Comment

by:Big Monty
ID: 39660117
do you have a page I can see?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 9

Author Comment

by:AdrienneSperber
ID: 39660138
0
 
LVL 32

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 52

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

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.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now