{"id":402,"date":"2023-03-30T22:22:02","date_gmt":"2023-03-31T01:22:02","guid":{"rendered":"http:\/\/fabiobrandao.net.br\/blog\/?p=402"},"modified":"2023-03-31T06:30:14","modified_gmt":"2023-03-31T09:30:14","slug":"jquery-preencher-dropdown-com-os-meses-do-ano","status":"publish","type":"post","link":"https:\/\/fabiobrandao.net.br\/blog\/javascript-e-bivliotecas\/jquery-preencher-dropdown-com-os-meses-do-ano\/","title":{"rendered":"JQuery &#8211; Preencher Dropdown com os meses do ano"},"content":{"rendered":"<p>Mas uma atividade trivial e muito simples de ser implementada.<\/p>\n<p>Implemente um dropdown e atribua a ele um identificador para poder manipul\u00e1-lo:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">&lt;select id=&quot;month&quot;&gt;&lt;\/select&gt;<\/pre>\n<p>Em seu arquivo de script, implemente um vetor contendo os nomes dos meses. Implementar um vetor permitir\u00e1 que voc\u00ea possa aproveitar estes valores futuramente.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar monthNames = &#x5B;\r\n    'Janeiro', 'Fevereiro', 'Mar\u00e7o', 'Abril', 'Maio', 'Junho', \r\n    'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'\r\n];\r\n<\/pre>\n<p>Agora implemente uma fun\u00e7\u00e3o como no exemplo abaixo que tem como objetivo, usar o vetor criado para preencher dinamicamente o dropdown.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction loadMonth() {\r\n   var dropdown = $('#month');\r\n   dropdown.empty();\r\n\r\n   dropdown.prop('selectedIndex', 0);\r\n\r\n   $.each(monthNames, function (key, entry) {\r\n      var option = $('&amp;lt;option \/&amp;gt;');\r\n      option.attr('value', key+1).text(entry);\r\n      dropdown.append(option);\r\n   });\r\n}\r\n<\/pre>\n<p>Por fim, implemente a chamada para a fun\u00e7\u00e3o. Esta chamada voc\u00ea pode implementar no local que seja melhor para seu contexto. Aqui estou fazendo a chamada assim que o documento estiver completamente carregado.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).ready(function () {\r\n   loadMonth();\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mas uma atividade trivial e muito simples de ser implementada. Implemente um dropdown e atribua a ele um identificador para poder manipul\u00e1-lo: &lt;select id=&quot;month&quot;&gt;&lt;\/select&gt; Em seu arquivo de script, implemente&#8230; <\/p>\n","protected":false},"author":1,"featured_media":377,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[69],"tags":[16,4,9,86],"class_list":["post-402","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-e-bivliotecas","tag-dropdown","tag-javascript","tag-jquery","tag-meses"],"_links":{"self":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/comments?post=402"}],"version-history":[{"count":10,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/402\/revisions\/412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/media\/377"}],"wp:attachment":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}