0

I have two dropdown lists and I want for example, if value=fetch selected then show second dropdown but it doesn't work.

I have tried with call external PHP file but I was stuck again. I think that the problem is when I call the on change method but I don't know what to type.

function action(dropdown) {

    var value = dropdown.options[dropdown.selectedIndex].value;
    if (value == 'Fetch'){
        document.getElementById("student_list").style.display = "block";
        document.getElementById("subject_list").style.display = "none";
    }

    if(value == 'Count'){
       document.getElementById("student_list").style.display = "block";
    }
    if(value == 'Percentage'){
       document.getElementById("subject_list").style.display = "block";
    }
}

<body>

  <div class="main_container">
    
        <form method="post">
            <select name="action" id ="fetch"  onchange="action(this.value);">
                <option value="" id="action">--Select Action--</option>
                <option value="Fetch">Fetch</option>
                <option value="Count">Count</option>
                <option value="Percentage">Percentage</option>
            </select>
            <select name="student_name" id ="student_list"  onChange="" style="display:none;">  <!--onchange="getNext(this.value);" -->
                <option value="">--Select  Action --</option>
                <option value="All">All Students</option>
                <option value="NameS">Student Name</option>
            </select>               
        
</form>
</div>
halfer
  • 19,824
  • 17
  • 99
  • 186
Debbie B
  • 13
  • 1

1 Answers1

1

You cannot use a function named "action" please see https://stackoverflow.com/a/37590405/5391965

You should retrieve your values within the function instead of passing it in parameters like so :

function displayStudentList(dropdown) {
    let subject = document.getElementById("subject_list").value;
    if (subject === 'Fetch') {
        document.getElementById("student_list").style.display = "block";
        document.getElementById("subject_list").style.display = "none";
    }
    if (subject === 'Count') {
        document.getElementById("student_list").style.display = "block";
    }
    if (subject === 'Percentage') {
        document.getElementById("subject_list").style.display = "block";
    }
}
<div class="main_container">
    <form method="post">
        <select name="action" id="subject_list"  onchange="displayStudentList()">
            <option value="" id="action">--Select Action--</option>
            <option value="Fetch">Fetch</option>
            <option value="Count">Count</option>
            <option value="Percentage">Percentage</option>
        </select>
        <select name="student_name" id ="student_list"  onChange="" style="display:none;">  
            <!--onchange="getNext(this.value);" -->
            <option value="">--Select  Action --</option>
            <option value="All">All Students</option>
            <option value="NameS">Student Name</option>
        </select>
    </form>
</div>
Reynadan
  • 649
  • 4
  • 18