Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 42 lines (42 sloc) 1.976 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?xml version="1.0"?>
<entry type="method" name="jQuery.inArray" return="Number">
  <title>jQuery.inArray()</title>
  <signature>
    <added>1.2</added>
    <argument name="value" type="Anything">
      <desc>The value to search for.</desc>
    </argument>
    <argument name="array" type="Array">
      <desc>An array through which to search.</desc>
    </argument>
    <argument name="fromIndex" type="Number" optional="true">
      <desc>The index of the array at which to begin the search. The default is 0, which will search the whole array.</desc>
    </argument>
  </signature>
  <desc>Search for a specified value within an array and return its index (or -1 if not found).</desc>
  <longdesc>
    <p>The <code>$.inArray()</code> method is similar to JavaScript's native <code>.indexOf()</code> method in that it returns -1 when it doesn't find a match. If the first element within the array matches <code>value</code>, <code>$.inArray()</code> returns 0.</p>
    <p>Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of <code>value</code> within <code>array</code>, we need to check if it's not equal to (or greater than) -1.</p>
  </longdesc>
  <example>
    <desc>Report the index of some elements in the array.</desc>
    <code><![CDATA[var arr = [ 4, "Pete", 8, "John" ];
var $spans = $("span");
$spans.eq(0).text(jQuery.inArray("John", arr));
$spans.eq(1).text(jQuery.inArray(4, arr));
$spans.eq(2).text(jQuery.inArray("Karl", arr));
$spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
]]></code>
    <css><![CDATA[
div { color:blue; }
span { color:red; }
]]></css>
    <html><![CDATA[
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<div>"Pete" is in the array, but not at or after index 2, so <span></span></div>]]></html>
  </example>
  <category slug="utilities"/>
  <category slug="version/1.2"/>
</entry>
Something went wrong with that request. Please try again.