SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic dropdown js rest some variables

    Hi all,

    I am using a script that for multiple dynamic dropdowns. This is what my form does.....

    I have a state, county, and department field, each a dropdown that can be dynamically populated.


    When a state is selected a list of counties associated with the state populate the second dropdown. When a county is selected a list of departments within the county populate the third dropdown.

    If the user selects a state and county my url looks like this...

    ?state=3247&county=323&source=0

    If the user decides to select a different state from here and the page reloads this is what my url looks like...

    ?state=3259&county=323&source=0

    Notice the county value remains unchanged.

    Here is the javascript I am using. How can I tweek it so that when a parent value changes the dependent values are reset to 0?

    Code:
    <SCRIPT language="JavaScript">
    function reload(form)
    {
    var val1=form.state.options[form.state.options.selectedIndex].value;
    var val2=form.County_name.options[form.County_name.options.selectedIndex].value;
    var val3=form.Gis_source.options[form.Gis_source.options.selectedIndex].value;
    self.location='/dataservices/gis_popup.php?state=' + val1 +'&county=' + val2 +'&source=' + val3;
    }
    </script>
    Thanks in advance.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The title should have been "reset" not "rest"

    So this is my workaround but I was hoping for something more compact. As you can see I just broke it down into 3 different functions. Is there any way to do it in just 1? I tried using some if-else statements but none of them were successful.


    Code:
    function reload_state(form)
    {
    var val1=form.state.options[form.state.options.selectedIndex].value;
    var val2=0;
    var val3=0;
    self.location='/dataservices/gis_popup.php?state=' + val1 +'&county=' + val2 +'&source=' + val3;
    
    }
    </script>
    <SCRIPT language="JavaScript">
    function reload_county(form)
    {
    var val1=form.state.options[form.state.options.selectedIndex].value;
    var val2=form.County_name.options[form.County_name.options.selectedIndex].value;
    var val3=0;
    self.location='/dataservices/gis_popup.php?state=' + val1 +'&county=' + val2 +'&source=' + val3;
    }
    </script>
    <SCRIPT language="JavaScript">
    function reload_source(form)
    {
    var val1=form.state.options[form.state.options.selectedIndex].value;
    var val2=form.County_name.options[form.County_name.options.selectedIndex].value;
    var val3=form.Gis_source.options[form.Gis_source.options.selectedIndex].value;
    self.location='/dataservices/gis_popup.php?state=' + val1 +'&county=' + val2 +'&source=' + val3;
    }
    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •