0

I'm trying to fire a function if an input field's value is equal to that of a number in an array.

I have my code below, but it doesn't seem to work!!

var num1 = $("#people-number").val();
var Numberarray = [1,3,5,7,9];

if ($.inArray(num1,Numberarray) > -1) {
     $("#valid-people").hide();
     $("#non-valid-people").fadeIn();   
}

Any tips would be appreciated.....

3 Answers 3

1

It is because the array has int values and the the value you are testing is a string

$.inArray(parseInt(num1),Numberarray)

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

You need to cast num1 to number.

if ($.inArray(+num1,Numberarray) > -1)

or

if ($.inArray(parseInt(num1, 10),Numberarray) > -1)

Comments

0

Test it with:

var num1 = $("#people-number").val(); var Numberarray = [1,3,5,7,9];

if ($.inArray(parseInt(num1),Numberarray) > -1) {
    $("#valid-people").hide();
    $("#non-valid-people").fadeIn();   
}

And return any result. Here it works: http://jsfiddle.net/fjorgemota/q6WJA/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.