egr103 egr103 - 28 days ago 13
Javascript Question

Multiple select value if statement jQuery

I have 2 select menus and i'm trying to compare a selected value of one with another but I cannot get the conditional statement working, please see my comments in my code below and in the jsFiddle.

Here's my code and a jsFiddle:

The expected value from the

#purpose
select menu is "5". The
var selected
is my attempt at grabbing that value from the
#purpose
select menu.

$('#purpose').on('change', function() {
if ( this.value == '8') {
$("#business").show();
$("#investor-type").hide();
} else {
$("#business").hide();
$("#investor-type").show();
}
});

var selected = $("#purpose option:selected", this).val();

$('#investor-type').on('change', function() {
if ( this.value == '1') {
$("#disclaimer-form").show();
$(".disclaimer-content").show();
// Having trouble with this else if below
} else if ( this.value == "2" && selected == "5") {
$("#disclaimer-form").show();
$(".disclaimer-content").show();
} else {
$("#disclaimer-form").hide();
$(".disclaimer-content").hide();
}
});

Answer

https://jsfiddle.net/mplungjan/Lea13zx1/

1 . move the selected inside the change

$('#investor-type').on('change', function() { var selected = $("#purpose").val();

2 . test the 5 inside the 2

else if (this.value == "2") { $("#retailstatusyes").toggle(selected == "5"); $("#prostatus").hide(); }