2010-09-16 17:13:38 +00:00
<!DOCTYPE html>
2010-11-02 01:46:29 +00:00
< html lang = 'en' > < head >
2011-02-18 20:00:18 +00:00
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, minimum-scale=1, maximum-scale=1" > < meta http-equiv = 'content-type' content = 'text/html; charset=UTF-8' / > < / head > < body >
2010-09-18 16:20:35 +00:00
< div data-role = "page" >
2010-09-21 14:58:54 +00:00
< div data-role = "header" >
2010-09-16 17:13:38 +00:00
< h1 > jQuery.inArray()< / h1 >
< / div >
2010-10-12 19:50:28 +00:00
< div data-role = "content" data-theme = "c" id = "jQuery.inArray1" >
2010-09-16 17:13:38 +00:00
< h2 class = "jq-clearfix roundTop section-title" >
< span class = "name" > jQuery.inArray( value, array )< / span > < span class = "returns" > Returns: < a class = "return" href = "http://docs.jquery.com/Types#Number" > Number< / a > < / span >
< / h2 >
< div class = " entry-details" >
< p class = "desc" > < strong > Description: < / strong > Search for a specified value within an array and return its index (or -1 if not found).< / p >
< ul class = "signatures" > < li class = "signature" id = "jQuery.inArray-value-array" >
< h4 class = "name" >
< span class = "versionAdded" > version added: < a href = "/category/version/1.2/" > 1.2< / a > < / span > jQuery.inArray( value, array )< / h4 >
< p class = "arguement" > < strong > value< / strong > The value to search for.< / p >
< p class = "arguement" > < strong > array< / strong > An array through which to search.< / p >
< / li > < / ul >
< div class = "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 >
< / div >
< h3 > Example:< / h3 >
< div id = "entry-examples" class = "entry-examples" > < div id = "example-0" >
< h4 > < span class = "desc" > Report the index of some elements in the array.< / span > < / h4 >
< pre > < code class = "example demo-code" > < !DOCTYPE html>
< html>
< head>
< style>
div { color:blue; }
span { color:red; }
< /style>
< script src="http://code.jquery.com/jquery-latest.js"> < /script>
< /head>
< body>
< div> "John" found at < span> < /span> < /div>
< div> 4 found at < span> < /span> < /div>
< div> "Karl" not found, so < span> < /span> < /div>
< script> var arr = [ 4, "Pete", 8, "John" ];
$("span:eq(0)").text(jQuery.inArray("John", arr));
$("span:eq(1)").text(jQuery.inArray(4, arr));
$("span:eq(2)").text(jQuery.inArray("Karl", arr));
< /script>
< /body>
< /html> < / code > < / pre >
< h4 > Demo:< / h4 >
< div class = "demo code-demo" > < / div >
< / div > < / div >
< / div >
< / div >
< / div >
< / body > < / html >