0

I would like to use a onchange state on a select element to check if the value of the select is in an array.

I tried this :

function statutEmployeur(){
   statliste = [];
   statliste.push(1);
   statliste.push(2);
   statliste.push(5);
   statid = jQuery("#statut").val();
   if(jQuery.inArray(statid,statliste)>-1){
      alert('inside array');
   }else {
      alert('outside array');
   }
}

Each value that I tried are outside of the array. Anybody can help me ?

Thanks

2
  • hey there i'd love to help you, mind throwing your current code (html,js) up into jsbin? makes it easier to give you specific advice. Commented Nov 17, 2015 at 16:08
  • 2
    Is this possibly a type issue? "1" is not considered to be in an array of [1, 2, 5] Commented Nov 17, 2015 at 16:08

1 Answer 1

1

as pointed out by Stryner in the comments, try running your .val() through parseInt() to ensure you nave a numerical value (and not a string)

   statid = jQuery("#statut").val();

to:

   statid = parseInt(jQuery("#statut").val(), 10);

working fiddle: http://jsbin.com/maqesupuka/edit?html,js,console,output

the problem is you're searching in the array for a type of string (default type of inputs), but your array only contains numbers. You could either make your array into numbers, or do a type conversion like above to ensure you're working with the same type.

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

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.