Saturday, 29 October 2016

jquery if variable in a list of values

I have some jquery checking the choice made in a drop down.



function toggleFields() {
if ($("#chosenmove1").val() == 4)
$("#hideme").show();
else

$("#hideme").hide();
}


This works fine but I would like to change it so it checks a list of values like



if ($("#chosenmove1").val() in (3,4,5))


How can I write this to make it work (the above doesn't)




Tried a bit more



var arr = [3,4,5];
var value = $("#chosenmove1").val();
alert(value);

if ($.inArray(value, arr) > -1)
$("#hideme").show();
else

$("#hideme").hide();

}


The alert box tells me var value is getting the right value from the drop down - yet the show hide wont work under this setup.
IF I replace var value = $("#chosenmove1").val(); with
var value = 3; then it does work?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...