user4676307 user4676307 - 2 months ago 14x
Javascript Question

Jquery Array Contains

I have an array that all the name of all selected check boxes. How can I check if that array contains specific value other than given string.

var selected_options = $('input[checked=checked]').map( function() {
return $(this).attr('name');

How can I check if the array selected_options has other elements other than lets say 'CancelTerms'?


I don't think there is the need to create an array for that. Just filter out element not having a certain name value using attribute equals selector with :not() pseudo-class selector ( or not() method ) and get its length.

if($('input[checked=checked]:not([name="CancelTerms"])') .length > 0){
   // code

If you would like to do it with the array then use Array#some method.

if(selected_options.some(function(v){ return v != "CancelTerms"; })){
   // code

or Array#filter method can be used.

if(selected_options.filter(function(v){ return v!= "CancelTerms"; }).legth){
   // code