Solved

2 drop downs using onchange, want to keep both selections

Posted on 2013-11-19
7
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 33

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 33

Expert Comment

by:Big Monty
ID: 39660117
do you have a page I can see?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 9

Author Comment

by:AdrienneSperber
ID: 39660138
0
 
LVL 33

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 53

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

739 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