1

I have 2 separate dropdown lists and submit button which takes the visitor to a unique URL based on selections.

I need to get each dropdown to filter each other.

for example: When someone chooses "Volvo" from the first dropdown menu, then the second drop-down menu shows only "Red" & "White" and so on.

HTML

<form>
  <select class="homepage-dropdown col-md-6 col-9" id="carbrand">
    <option value="volvo">Volvo</option>
    <option value="saab" selected="selected">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
<br>
  <select class="homepage-dropdown col-md-6 col-9" id="carcolor">
    <option value="white">White</option>
    <option value="black" selected="selected">Black</option>
    <option value="red">Red</option>
    <option value="blue">Blue</option>
  </select>

  <br/><br/>

  <input id="btnCallURL" type="button" value="بحث">  
</form>

Javascript

document.getElementById('btnCallURL').onclick = function() { 
    var carbrandObj = document.getElementById('carbrand');
    var carcolorObj = document.getElementById('carcolor');

    var carbrandValue = carbrandObj.options[carbrandObj.selectedIndex].value;
    var carColorValue = carcolorObj.options[carcolorObj.selectedIndex].value;

    jsCallUrl(carbrandValue, carColorValue);
}

function jsCallUrl(carbrandValue, carColorValue){

    var pageUrl = "";

    switch (carbrandValue){
        case "volvo":
            switch (carColorValue){
            case "white":
                pageUrl = "volvo-white-page-url.php";
                break;
            case "black":
                pageUrl = "volvo-black-page-url.php";
                break;
         case "red":
                pageUrl = "volvo-red-page-url.php";
                break;
         case "blue":
                pageUrl = "volvo-blue-page-url.php";
            break;            
          }
          break;
        case "saab":
            switch (carColorValue){
            case "white":
                pageUrl = "saab-white-page-url.php";
                break;
            case "black":
                pageUrl = "saab-black-page-url.php";
                break;
            default:
                pageUrl = "saab-othercolor-page-url.php";
                break;
          }
          break;
    }



    if (pageUrl != ""){
      location.href = "https://domain/" + pageUrl;
    }
}
3
  • are you comfortable with a jquery solution. Commented Apr 23, 2019 at 7:50
  • Please update it if you see that it can be written better. because I'm new in this Commented Apr 23, 2019 at 8:33
  • check the new solution Commented Apr 23, 2019 at 9:27

1 Answer 1

3

Edit the problem of repeating values is sorted. I've made a few changes to your HTML

    //new
window.addEventListener( "pageshow", function ( event ) {
          var historyTraversal = event.persisted || ( typeof window.performance != "undefined" && window.performance.navigation.type === 2 );
          if ( historyTraversal ) {
            window.location.reload();
          }
        });
<form>
  <select  onChange="populate('carbrand','carcolor');" class="homepage-dropdown col-md-6 col-9" id="carbrand">
     <option selected>select</option> 
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
<br>
  <select class="homepage-dropdown col-md-6 col-9" id="carcolor">
    <option id="select" selected>Select color</option>
  </select>
  <br/><br/>
  <input id="btnCallURL" type="button" value="بحث">  
</form>

And I've used Populate method to enter the values dynamically using Javascript.

<script>
function populate(s1,s2){
            var s = document.getElementById(s1);
            var s_a = document.getElementById(s2);


    var select = document.getElementById(s2).options.length;
    for (var i = select; i >0;i-- ) {
        document.getElementById(s2).options.remove(i);
        console.log(i);
    }
     document.getElementById('select').selected='selected';
  var optionArray=[];
            if(s.value == "volvo"){
                 optionArray = ["red|Red","white|White"];
            } 
      else if(s.value == "saab"){
      optionArray = ["white|White","gray|Gray"];
      }
      else if(s.value == "audi"){
       optionArray = ["black|Black","red|REd"];
      }
      else if(s.value == "opel"){
      optionArray = ["red|REd","black|Black"];
      }
            for(var option in optionArray){
                var pair = optionArray[option].split("|");
                var newOption = document.createElement("option");
                newOption.value = pair[0];
                newOption.innerHTML = pair[1];
                s_a.options.add(newOption);
            }
        }
document.getElementById('btnCallURL').onclick = function() { 
    var carbrandObj = document.getElementById('carbrand');
    var carcolorObj = document.getElementById('carcolor');

    var carbrandValue = carbrandObj.options[carbrandObj.selectedIndex].value;
    var carColorValue = carcolorObj.options[carcolorObj.selectedIndex].value;

    jsCallUrl(carbrandValue, carColorValue);
}

function jsCallUrl(carbrandValue, carColorValue){

    var pageUrl = "";

    switch (carbrandValue){
        case "volvo":
            switch (carColorValue){
            case "white":
                pageUrl = "volvo-white-page-url.php";
                break;
            case "black":
                pageUrl = "volvo-black-page-url.php";
                break;
         case "red":
                pageUrl = "volvo-red-page-url.php";
                break;
         case "blue":
                pageUrl = "volvo-blue-page-url.php";
            break;            
          }
          break;
        case "saab":
            switch (carColorValue){
            case "white":
                pageUrl = "saab-white-page-url.php";
                break;
            case "black":
                pageUrl = "saab-black-page-url.php";
                break;
            default:
                pageUrl = "saab-othercolor-page-url.php";
                break;
          }
          break;
    }



    if (pageUrl != ""){
      location.href = "https://domain/" + pageUrl;
    }
}
    </script>
Sign up to request clarification or add additional context in comments.

3 Comments

great, the repeating problem has been sorted, but the button is not working.
check now, it was because the value to option was not completely in lower case
great now it's working fine. Only last note that when the button clicked and the visitor go to another page, then click the "back" button the form is not working properly. do you have a fix for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.