{"id":675,"date":"2012-05-30T07:43:12","date_gmt":"2012-05-30T07:43:12","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=675"},"modified":"2012-05-30T07:43:12","modified_gmt":"2012-05-30T07:43:12","slug":"jquery-get-selected-index-jquery-get-selected-option-index-2-ways","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-get-selected-index-jquery-get-selected-option-index-2-ways\/","title":{"rendered":"jQuery Get Selected Index Examples &#8211; 2 Ways"},"content":{"rendered":"<p>At times, it could be very useful to use <strong>jQuery to\u00a0Get Selected Option Index value<\/strong> for carrying out relevant processing. In this article, I am going to show you a very easy and simple way to <em><strong>get selected option index value using jQuery<\/strong><\/em>. And the actual code is just 1 line, so it&#8217;s super easy to follow and implement.<!--more--><\/p>\n<h2>Examples to Get Selected Option Index 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=675\" 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 you have a select dropdown with ID &#8220;countries&#8221;. To get the index of the selected option, you can use any of the following 2 methods:<\/p>\n<p><strong>Method 1: By using the &#8216;ID&#8217; of the select dropdown:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">$(\"#countries option:selected\").index();<\/pre>\n<p><strong>Method 2:\u00a0By using the &#8216;name&#8217; of the select dropdown:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">$(\"select[name='countries'] option:selected\").index();<\/pre>\n<h3>Example: Get Selected Option Index using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Examples to use jQuery to Get Selected Option Index&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;select name=\"countries\" id=\"countries\"&gt;\n  &lt;option value=\"Australia\"&gt;Australia&lt;\/option&gt;\n  &lt;option value=\"London\"&gt;London&lt;\/option&gt;\n  &lt;option value=\"USA\"&gt;USA&lt;\/option&gt;\n&lt;\/select&gt;\n&lt;input type=\"submit\" name=\"get\" id=\"get\" value=\"Get selected index\" \/&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$('#get').click(function() {\n\n \t\/\/Method 1\n\talert( $(\"#countries option:selected\").index() );\n\n\t\/\/Method 2\n\talert( $(\"select[name='countries'] option:selected\").index() );\n\n});\n\n&lt;\/script&gt; \n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\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=675\" 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>Very simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to use<em><strong> jQuery to get selected index \/ get selected option index<\/strong><\/em> ? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At times, it could be very useful to use jQuery to\u00a0Get Selected Option Index value for carrying out relevant processing. In this article, I am going to show you a very easy and simple way to get selected option index value using jQuery. And the actual code is just 1 line, so it&#8217;s super easy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"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,41],"class_list":["post-675","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-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-aT","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/675","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=675"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/675\/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=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}