Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2 drop downs using onchange, want to keep both selections

Posted on 2013-11-19
7
Medium Priority
?
322 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 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 33

Expert Comment

by:Big Monty
ID: 39660117
do you have a page I can see?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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…

730 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