mercredi 16 décembre 2015

Javascript, check if input is in array

I am new to javascript and struggling with the use of arrays.

I am trying to check if user input value is in an array I declared called fruits. If it is I want to execute code. If it is not I want an alert displayed. I tried using the

instanceof

method to check the value but the code doesn't execute any of the if else statement. Any ideas as to why?

$("#submit-btn").bind("click", function() {

           var comment = $("#comments");
            var commentValue = $.trim(comment.val());
            var index;
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            for (index = 0; index < fruits.length; index++) {
                text += fruits[index];


            if (commentValue.length === 0) {
                alert('Comments are required to continue!');
            } 
            else if (commentValue instanceof fruits){
                execute code
                });
            }
            else {
                alert('not a valid fruit');
            }

            return false;
            }
        });

Aucun commentaire:

Enregistrer un commentaire