Compare two dropdown values

I need a function that will compare two values ​​from two dropdown selectors, and if they are the same, show a div. I would like to use jQuery if possible.

<select id="drop1">
   <option value="a">a
   <option value="b">b
   <option value="c">c
</select>

<select id="drop2">
   <option value="a">a
   <option value="b">b
   <option value="c">c
</select>
+3
source share
2 answers
$("#myDiv").toggle($("#drop1").val() === $("#drop2").val());

Explanation: $("#dropX").val()Gets the value of the selected item in this drop-down menu; ===compares them, giving trueor falseas necessary; and $("myDiv").toggle(...)shows or hides #myDivdepending on the passed value.

If you want to do this when changing the value, wrap it in $("#drop1, #drop2").change(function () { ... });as in nickf's answer.

+6
source
$('#drop1, #drop2').change(function() {
    $('#myDiv').toggle(
        $('#drop1').val() === $('#drop2').val()
    );
});
+5
source

Source: https://habr.com/ru/post/1780730/


All Articles