Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:07:11+00:00 2026-06-17T15:07:11+00:00

I am having an issue where jQuery.InArray() is returning -1 when I run a

  • 0

I am having an issue where jQuery.InArray() is returning -1 when I run a function, but I know that the values that I’m testing for are definitely in the array.

I know that they are in the array because if I console.log the values of the array right before I run jQuery.inArray() the values exist and display. Here is an example of what I’m working with:

/* -- In page.php -- */
images = [8, 13, 21, 32, 40, 56]; 


/* -- In script.js -- */
console.log('images->', images); 
//output  images-> [8, 13, 21, 32, 40, 56]

var testingValue = 13;

console.log('inArray->', jQuery.inArray(testingValue, images)); //Should return 1
//output inArray-> -1

console.log('inArray->', jQuery.inArray(13, images)); //Should return 1
//output inArray-> -1

console.log('inArray->', jQuery.inArray("13", images)); //Should return -1
//output inArray-> -1

console.log('inArray->', jQuery.inArray('13', images)); //Should return -1 
//output inArray-> -1

All of these return -1 even though the top two should return 1. The weird thing is that if I manually test for jQuery.inArray(testingValue, images) in the console, I do get the value 1.

One thing to note: The variable images is created in the php page that the js file is included in. This shouldn’t be an issue.

Additional note: Yes, jQuery is included well before this part of the script runs. Many other jQuery functions are running successfully before this one.

What I am wondering is if there are any gotchas to this function. I have used it successfully in the past and can’t see anything with my code which would prevent this from returning a proper value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:07:12+00:00Added an answer on June 17, 2026 at 3:07 pm

    After a good nights rest I was able to circle up and take a fresh look at the problem. The variable testingValue = 13; is explicitly cast as an integer. The problem was that I was getting that value dynamically through the jQuery $.each(arrayName, function(index,value){}). Although the data showed that index was indeed an integer, for some reason $.inArray() was not treating it like an int/float and therefore was not finding a value in the numeric array. I modified my code to parseInt() the index variable before checking that it was inArray.

    Long Story Short

    If you are having trouble trying to use jQuery.inArray() with an array of integers or floats, try using parseInt() or parseFloat(), respectively, to ensure that the function is receiving the correct data type.

    if( jQuery.inArray(parseInt(yourVariable), yourArray) > -1)
    {
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am having issue with returning some values from PHP to jQuery -
I am having an issue with a jquery function. It works in FF but
Having an issue with traversing in jQuery.. hopfully an easy one..but I'm banging my
I'm having an issue with two jQuery calls. The first is a load that
I am having an issue with jquery tablesorter I have a column that I
I am having an issue with jQuery's animate function and IE8. The problem is
I'm having an issue with jQuery in that when I receive the html response
I am having an issue with some Jquery script that I have and I
I'm having an issue with jQuery functions not running if a preceding function failed
I'm having an issue with the .each() function in jQuery, I'm calling these lines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.