Home » Jquery » javascript – How to change other country if i select one country in Jquery-Exceptionshub

javascript – How to change other country if i select one country in Jquery-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

i have two select box. if i select Singapore in select box 1, i want to show Indonesia in select box 2 and singapore not display in select box 2. please see the picture enter image description here
this is html code :

<div class="input-group select2-bootstrap-prepend">
                <span class="btn" style="width:41px; border-radius: 0.25rem 0 0 0.25rem; background-color:#3b5998; color:white; border-right:none;"><i class="fas fa-ship fa-sm"></i></span>
                <select class="custom-select" id="asal">
                    <optgroup label="Singapore">
                        <option value="1">HarbourFront</option>
                        <option value="2">Tanah Merah</option>
                    </optgroup>
                    <optgroup label="Indonesia">
                        <option value="1">Batam Center</option>
                        <option value="2">Harbour Bay</option>
                        <option value="3">Nongsa</option>
                        <option value="3">Sekupang</option>
                        <option value="3">Tanjung Balai</option>
                        <option value="3">Tanjung Pinang</option>
                    </optgroup>
                </select>
            </div>
            <label class="mt-4" for="Tujuan-keberangkatan">Tujuan Keberangkatan</label>
            <div class="input-group mb-4 select2-bootstrap-prepend">
                <span class="btn" style="width:41px; border-radius: 0.25rem 0 0 0.25rem; background-color:#3b5998; color:white; border-right:none;" class="input-group-addon"><i class="fas fa-ship fa-sm"></i></span>
                <select class="custom-select" id="tujuan">
                    <optgroup label="Singapore">
                        <option value="1">HarbourFront</option>
                        <option value="2">Tanah Merah</option>
                    </optgroup>
                    <optgroup label="Indonesia">
                        <option value="1">Batam Center</option>
                        <option value="2">Harbour Bay</option>
                        <option value="3">Nongsa</option>
                        <option value="3">Sekupang</option>
                        <option value="3">Tanjung Balai</option>
                        <option value="3">Tanjung Pinang</option>
                    </optgroup>
                </select>
            </div>
How to&Answer:

Using jQuery onchange event, you can get the selected value and use it to get optgroup label, according to the label we can hide/show second select values, check the below example which uses jQuery and shows/hides the Singapore/Indonesia values according to the first selectbox value.

$(document).ready(function(){
  $("#asal").on("change", function(){
  var asallbl = $("#asal :selected").parent().attr("label");
  if(asallbl === "Singapore"){
    $("#tujuan").find("optgroup[label='Indonesia']").show();
    $("#tujuan").find("optgroup[label='Singapore']").hide();
    $("#tujuan").prop("selectedIndex", 0);
  }else{
    $("#tujuan").find("optgroup[label='Indonesia']").hide();
    $("#tujuan").find("optgroup[label='Singapore']").show();
    $("#tujuan").prop("selectedIndex", 0);
  }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-group select2-bootstrap-prepend">
                <span class="btn" style="width:41px; border-radius: 0.25rem 0 0 0.25rem; background-color:#3b5998; color:white; border-right:none;"><i class="fas fa-ship fa-sm"></i></span>
                            <label class="mt-4" for="Tujuan-keberangkatan">Asal Keberangkatan</label><br/>
                <select class="custom-select" id="asal">
                    <option value="0">Choose Asal</option>
                    <optgroup label="Singapore">
                        <option value="1">HarbourFront</option>
                        <option value="2">Tanah Merah</option>
                    </optgroup>
                    <optgroup label="Indonesia">
                        <option value="1">Batam Center</option>
                        <option value="2">Harbour Bay</option>
                        <option value="3">Nongsa</option>
                        <option value="3">Sekupang</option>
                        <option value="3">Tanjung Balai</option>
                        <option value="3">Tanjung Pinang</option>
                    </optgroup>
                </select>
            </div>
            <label class="mt-4" for="Tujuan-keberangkatan">Tujuan Keberangkatan</label>
            <div class="input-group mb-4 select2-bootstrap-prepend">
                <span class="btn" style="width:41px; border-radius: 0.25rem 0 0 0.25rem; background-color:#3b5998; color:white; border-right:none;" class="input-group-addon"><i class="fas fa-ship fa-sm"></i></span>
                <select class="custom-select" id="tujuan">
                <option value="0">Choose Tujuan</option>
                    <optgroup label="Singapore">
                        <option value="1">HarbourFront</option>
                        <option value="2">Tanah Merah</option>
                    </optgroup>
                    <optgroup label="Indonesia">
                        <option value="1">Batam Center</option>
                        <option value="2">Harbour Bay</option>
                        <option value="3">Nongsa</option>
                        <option value="3">Sekupang</option>
                        <option value="3">Tanjung Balai</option>
                        <option value="3">Tanjung Pinang</option>
                    </optgroup>
                </select>
            </div>