{"id":3864,"date":"2013-05-18T19:05:18","date_gmt":"2013-05-18T19:05:18","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3864"},"modified":"2013-05-18T19:05:18","modified_gmt":"2013-05-18T19:05:18","slug":"jquery-search-array-for-value","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-search-array-for-value\/","title":{"rendered":"jQuery Find Value In Array Examples"},"content":{"rendered":"<p>Talking about arrays in jQuery, it can be really helpful to know if a value exists in an array or not. That said, in this article, I am going to share examples on how to <strong>find if a specific value exists in an array or not using jQuery<\/strong>. Try the demo to see how it works in real time.<\/p>\n<p><!--more--><\/p>\n<h2>How To Find If A Value Exists In Array Or Not Using jQuery<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3864\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have an array of fruits. Now we would like to search if this array contains a fruit of our choice. Let&#8217;s see how this can be done.<\/p>\n<h3>Example: Check if a value exists in an array when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;input type=\"submit\" name=\"sbt_check\" id=\"sbt_check\" value=\"Check if 'mango' exists in array\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_check_straw\" id=\"sbt_check_straw\" value=\"Check if 'strawberry' exists in array\"&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function(){\n\nvar fruits_array = new Array('grapes', 'apple', 'mango');\t\n\n$('#sbt_check').on(\"click\", function(e){\n\n\tvar search_for = 'mango';\n\n\tif($.inArray(search_for, fruits_array) &gt; -1)\n\t{\n\t\talert(\"Mango exists in the array\");\n\t}\n\telse\n\t{\n\t\talert(\"Mange does not exist in array\");\n\t}\n\n\te.preventDefault();\n\n});\n\n$('#sbt_check_straw').on(\"click\", function(e){\n\n\tvar search_for = 'strawberry';\n\n\tif($.inArray(search_for, fruits_array) &gt; -1)\n\t{\n\t\talert(\"Strawberry exists in the array\");\n\t}\n\telse\n\t{\n\t\talert(\"Strawberry does not exist in array\");\n\t}\n\n\te.preventDefault();\n\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>In the above example, when the first button is clicked, the value &#8216;mango&#8217; is searched for and the result is displayed. When\u00a0the second button is clicked, the value &#8216;strawberry&#8217; is searched for and the result is displayed.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3864\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to find value in array of objects using jQuery? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Talking about arrays in jQuery, it can be really helpful to know if a value exists in an array or not. That said, in this article, I am going to share examples on how to find if a specific value exists in an array or not using jQuery. Try the demo to see how it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[42,43,41],"class_list":["post-3864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-10k","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3864"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}