Solved

2 drop downs using onchange, want to keep both selections

Posted on 2013-11-19
7
304 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
Comment Utility
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
Comment Utility
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
Comment Utility
do you have a page I can see?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:AdrienneSperber
Comment Utility
0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
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
Comment Utility
Got it to work, thank you so much!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore 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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

743 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

21 Experts available now in Live!

Get 1:1 Help Now