{"id":943,"date":"2012-06-20T04:36:50","date_gmt":"2012-06-20T04:36:50","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=943"},"modified":"2012-06-20T04:36:50","modified_gmt":"2012-06-20T04:36:50","slug":"jquery-set-dropdown-value-set-dropdown-value-using-jquery-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-set-dropdown-value-set-dropdown-value-using-jquery-examples\/","title":{"rendered":"jQuery Set Dropdown Value Examples"},"content":{"rendered":"<p>If you are wondering <em><strong>how to use jQuery to set dropdown value using jQuery<\/strong><\/em>, then look no further! In this article, I am going to share some examples that show how to easily set dropdown value.<\/p>\n<p><!--more--><\/p>\n<h2>How to use jQuery to Set Dropdown Value<\/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=943\" 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>The following is the HTML source code of the example. It assumes that there is a dropdown box with ID &#8220;country&#8221; and the \u00a0value of the dropdown needs to be set to a different value when a button is clicked.<\/p>\n<h3>Common HTML Code for all following examples:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jQuery Set Dropdown Value | Set Dropdown Value using jQuery Examples&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=\"country\" id=\"country\"&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=\"set\" id=\"set\" value=\"Set USA as country\" \/&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h3>Example 1: Set Dropdown Value using val() method using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#set').click(function() {\n\n$(\"#country\").val(\"USA\");\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 2: Set Dropdown Value using\u00a0attr\u00a0selectedIndex using jQuery (Works only when jQuery version &lt; 1.6)<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#set').click(function() {\n\n$(\"#country\").attr('selectedIndex', 2);\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 3: Set Dropdown Value using\u00a0attr\u00a0selectedIndex using jQuery (Works only when jQuery version &gt;= 1.6)<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#set').click(function() {\n\n$(\"#country\").prop('selectedIndex', 2);\n\n});\n\n&lt;\/script&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=943\" 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>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to <em><strong>set dropdown value using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are wondering how to use jQuery to set dropdown value using jQuery, then look no further! In this article, I am going to share some examples that show how to easily set dropdown value.<\/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,84,85,41],"class_list":["post-943","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-dropdown","tag-jquery-set-dropdown-value","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-fd","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/943","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=943"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/943\/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=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}