Reputation: 69
in my validation the required fields can change. I have an array (required) of input ids which are required. If the array contains the string 'All' then all inputs are required.
I'm trying to use JQuery.inArray() to determine if required.
function getIfRequired(id){
console.log("id: "+id);
console.log(required);
console.log("inarray: "+jQuery.inArray(required, 'All'));
if (jQuery.inArray(required, 'All') > -1){ return true; }
else if(jQuery.inArray(required, id) != -1){ return true; }
else{ return false; }
}
But it keeps returning '-1' (not found).
here are example log outputs:
id: clientname
["clientname", "sourceid", "clientcountry","clienttown", "clienttownid", "typeoflaw"]
inarray: -1
id: clientname
["All"]
inarray: -1
Why is it not working?
Upvotes: 0
Views: 5221
Reputation: 4653
You call wrong jQuery.inArray
. First goes the value and then the array
see http://api.jquery.com/jQuery.inArray/
write
jQuery.inArray('All', required);
instead of
jQuery.inArray(required, 'All');
Upvotes: 1
Reputation: 15387
because required
not found in All
You will write as below
console.log("inarray: "+jQuery.inArray('All', required));
Upvotes: 0
Reputation: 33857
You have your value and array parameters transposed, e.g.:
jQuery.inArray('All', required);
Upvotes: 11