{"id":837,"date":"2012-06-07T14:01:56","date_gmt":"2012-06-07T14:01:56","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=837"},"modified":"2012-06-07T14:01:56","modified_gmt":"2012-06-07T14:01:56","slug":"jquery-select-value-get-select-value-with-jquery-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-select-value-get-select-value-with-jquery-examples\/","title":{"rendered":"jQuery Get Select Value Examples"},"content":{"rendered":"<p>Have you wondered how to use <em><strong>jQuery to\u00a0<\/strong><strong>get Select value<\/strong><\/em>? The select value can be easily obtained by using just single line of code. Follow along for more info.<!--more--><\/p>\n<h2>How to Get Select Value with jQuery<\/h2>\n<p>Let&#8217;s assume that you have a Select with ID &#8220;states&#8221;. You can now get the selected option value using jQuery in the following ways:<\/p>\n<p><strong>Here is the HTML source for the following example:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;select name=\"states\" id=\"states\"&gt;\n&lt;option value=\"Texas\"&gt;Texas&lt;\/option&gt;\n&lt;option value=\"Alabama\"&gt;Alabama&lt;\/option&gt;\n&lt;option value=\"Chicago\"&gt;Chicago&lt;\/option&gt;\n&lt;\/select&gt;<\/pre>\n<h3>Example 1: Get Select value using val() on page load using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\nvar value = $(\"#states\").val();\n\nalert(value);\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>When you execute the above code, you will see a JavaScript alert the value of the first option as the selected . To assign some value to the Select and get it&#8217;s value when the page loads, try the HTML source below and execute the code:<\/p>\n<p><strong><strong>Here is the HTML source for the following example:<\/strong><\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;select name=\"states\" id=\"states\"&gt;\n&lt;option value=\"Texas\"&gt;Texas&lt;\/option&gt;\n&lt;option value=\"Alabama\"&gt;Alabama&lt;\/option&gt;\n&lt;option value=\"Chicago\" selected=\"selected\"&gt;Chicago&lt;\/option&gt;\n&lt;\/select&gt;<\/pre>\n<p>You will now see that the JavaScript alert says: Chicago<\/p>\n<p>So that&#8217;s how you can get Selected option value on document load.<\/p>\n<h3>Example 2: Get Select value when it&#8217;s value on change using change() in jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\"#states\").change(function(){\n\nvar value = $(this).val();\n\nalert(value);\n\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>What the above jQuery does is that it alerts the value of the Selected option as soon as a different option is selected, without any delay.<\/p>\n<h3>Example 3: Get Select value using it&#8217;s name attribute using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\"input[name='states']\").change(function(){\n\nvar value = $(this).val();\n\nalert(value);\n\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>If you have noticed, I am targeting the Select by using it&#8217;s name in the above code. That&#8217;s how you can get Select value using name in jQuery.<\/p>\n<h3>Example 4: Get Select value using it&#8217;s Class using jQuery<\/h3>\n<p><strong>HTML Source:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;input name=\"states\" type=\"text\" id=\"states\" class=\"states_class\" \/&gt;<\/pre>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\".states_class\").change(function(){\n\nvar value = $(this).val();\n\nalert(value);\n\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>In the above code, the Select is targeted using the &#8220;states_class&#8221; applied to the Select. So now whenever the content of the &#8220;states&#8221; Select is changed, its value will be shown in alert.<\/p>\n<h3>Example 5: Get Select value using blur() using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\"#states\").blur(function(){\n\nvar value = $(this).val();\n\nalert(value);\n\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>What the above jQuery does is that it alerts the value of the Select instanlty as it is changed. This works same as the change() method in this context.<\/p>\n<h5>That&#8217;s it for now!<\/h5>\n<p>Do you know of any other ways to <em><strong>get Select value using jQuery or to get selected value of select dropdown using jQuery<\/strong><\/em>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you wondered how to use jQuery to\u00a0get Select value? The select value can be easily obtained by using just single line of code. Follow along for more info.<\/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":[41],"class_list":["post-837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-dv","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/837","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=837"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/837\/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=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}