0

I'm trying to poplulate a select box that is dependent on another select box

<select>
    <option value="a">a</option>
    <option value="b">b</option>
</select>



<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

I want for example when a user selects the select1 (a), Select2 shows 1, 2 and 3 and when he chooses B, It shows 4,5,6 and so on.

The selectbox values is a PHP ARRAY object, I've tried jQuery to populate it using $.each but that didn't work out as the array is something like:

{ "Option 1":[ {"name" : "test", "age" : "1"}, .... ] }

Thanks in advance.

3
  • You have to create dynamic option elements based on select box 1 and append those elements in select box 2. Can you please share your complete solution. Commented Jul 22, 2017 at 15:19
  • jQuery may help you. Here is a working example with jsfiddle Commented Jul 22, 2017 at 15:24
  • Did not answers help you? Commented Jul 23, 2017 at 16:28

4 Answers 4

1

For example you have below json:

{
    "Apple": {
        "iphone SE": "https://www.apple.com/iphone-se/",
        "iphone 7": "https://www.apple.com/iphone-7/"
    },
    "samsung": {
        "galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
        "galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
    }
}

For this we must to use javascript and It's good to use jQuery(a library for javascript).Take a look at the example below:

$(function() {
    var selectValues = {
        "apple": {
            "iphone SE": "https://www.apple.com/iphone-se/",
            "iphone 7": "https://www.apple.com/iphone-7/"
        },
        "samsung": {
            "galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
            "galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
        }
    };

    var $vendor = $('select.mobile-vendor');
    var $model = $('select.model');
    $vendor.change(function() {
        $model.empty().append(function() {
            var output = '';
            $.each(selectValues[$vendor.val()], function(key, value) {
                output += '<option>' + key + '</option>';
            });
            return output;
        });
    }).change();

    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>  
    <select class="mobile-vendor">
    <option value="apple">Apple</option>
    <option value="samsung">Samsung</option>
  </select>

<select class="model">
    <option></option>
</select>
</p>

Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example of how to implement the change with jquery.

Please post/explain your objects format a little more and I will create a loop to parse it correctly.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<select id="letter">
    <option value="a">a</option>
    <option value="b">b</option>
</select>

<select id="age">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>
  <script>
  $("#letter").change(function(){
    if($("#letter").val() === "a"){
      $("#age")
        .empty()
        .append('<option selected="selected" value="1">1</option>')
      ;
    }else{
      $("#age")
        .empty()
        .append('<option selected="selected" value="5">5</option>')
      ;
    }
  });
  </script>
</body>
</html>

Comments

0
// you can try something like this.

var s1 = document.getElementById("s1");
var s2 = document.getElementById("s2");

var s1Value = s1.value;

var i;
var frag = document.createDocumentFragment();
var ele;
if(s1Value == 'a'){
    for(i=0;i<3;i++){
        ele = document.createElement('option');
        ele.value = i + 1;
        ele.innerHTML = i + 1;
        frag.appendChild(ele);
    }
}
s2.innerHTML = ''
s2.appendChild(frag);

Comments

0

Thanks guys for all the help however, I followed another approach. which is to output the whole PHP array in the select as options, and hide them all, and when an option in select1 is chosen, I use jQuery to only show the items that has a specific attribute:

<script>
$(".select2").prop("disabled", true);
$(".select1").change(function() {
$('.select2').prop('disabled', false);
$(".select2 option").hide();
var currentVal = $('.select1 :selected').attr('optionis'),
ValueSelected = $(".select2 option[optionvalue*='" + currentVal + "']");
ValueSelected.show();
$(ValueSelected).first().prop("selected", true); 
});
</script>
<select class="select1">
  <option value="1" optionis="1">Option 1</option>
  <option value="2" optionis="2">Option 2</option>
</select>
<select class="select2">
  <option value="1" optionvalue="1">Value 1</option>
  <option value="2" optionvalue="1">Value 2</option>
  <option value="1" optionvalue="2">Value 3</option>
  <option value="1" optionvalue="2">Value 4</option>
  <option value="1" optionvalue="2">Value 5</option>
</select>

CODEPEN

Comments

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.