santa
santa

Reputation: 12512

compare two drop down values

I need a function that will compare two values from two drop-down selectors and if they are the same show a div. I'd 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>

Upvotes: 3

Views: 6155

Answers (2)

nickf
nickf

Reputation: 546055

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

Upvotes: 5

Domenic
Domenic

Reputation: 112827

$("#myDiv").toggle($("#drop1").val() === $("#drop2").val());

Explanation: $("#dropX").val() gets the value of the selected element in that dropdown; the === compares them, giving true or false as appropriate; and $("myDiv").toggle(...) either shows or hides #myDiv depending on the passed value.

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

Upvotes: 6

Related Questions