Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Method of Calculating Number of Days in Month for Date Form Drop-down
    primarykey
    data
    text
    <p>I have coded the following in order to preset the number of days for each selected month of a form drop-down list. This also tests for leap year (from year 2000+ since I do not require any earlier). This code appears to work correctly for me, but is there a more efficient way of doing this in terms of less code?</p> <pre><code>&lt;script&gt; function is_leap() { var year=document.getElementById('f_year').value; var a = 2000; for(a;a&lt;year;a+=4) { } if(a==year) { return true; } else return false; } function out_days(v) { var html = ''; for(var x=1;x&lt;=v;x++) { html += '&lt;option value="' + x + '"&gt;' + x + '&lt;/option&gt;'; } document.getElementById('f_day').innerHTML=html; } function chk_mon() { var e = document.getElementById('f_month').selectedIndex; switch(e) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: { out_days(31); break; } case 1: { if(is_leap()) { out_days(29); } else out_days(28); break; } case 3: case 5: case 8: case 10: { out_days(30); break; } default: { out_days(31); break; } } } &lt;/script&gt; &lt;form action="index.php" method="post" name="form"&gt; Date:&lt;select onchange="chk_mon()" id="f_month" name="from_month"&gt; &lt;option value="1"&gt;January&lt;/option&gt; &lt;option value="2"&gt;February&lt;/option&gt; &lt;option value="3"&gt;March&lt;/option&gt; &lt;option value="4"&gt;April&lt;/option&gt; &lt;option value="5"&gt;May&lt;/option&gt; &lt;option value="6"&gt;June&lt;/option&gt; &lt;option value="7"&gt;July&lt;/option&gt; &lt;option value="8"&gt;August&lt;/option&gt; &lt;option value="9"&gt;September&lt;/option&gt; &lt;option value="10"&gt;October&lt;/option&gt; &lt;option value="11"&gt;November&lt;/option&gt; &lt;option value="12"&gt;December&lt;/option&gt; &lt;/select&gt; &lt;select id="f_day" name="from_day"&gt; &lt;script&gt; chk_mon(); &lt;/script&gt; &lt;/select&gt; &lt;select onchange="chk_mon()" id ="f_year" name="from_year"&gt; &lt;option value="2013"&gt;2013&lt;/option&gt; &lt;option value="2014"&gt;2014&lt;/option&gt; &lt;option value="2015"&gt;2015&lt;/option&gt; &lt;option value="2016"&gt;2016&lt;/option&gt; &lt;option value="2017"&gt;2017&lt;/option&gt; &lt;option value="2018"&gt;2018&lt;/option&gt; &lt;option value="2019"&gt;2019&lt;/option&gt; &lt;option value="2020"&gt;2020&lt;/option&gt; &lt;option value="2021"&gt;2021&lt;/option&gt; &lt;option value="2022"&gt;2022&lt;/option&gt; &lt;option value="2023"&gt;2023&lt;/option&gt; &lt;option value="2024"&gt;2024&lt;/option&gt; &lt;option value="2025"&gt;2025&lt;/option&gt; &lt;option value="2026"&gt;2026&lt;/option&gt; &lt;option value="2027"&gt;2027&lt;/option&gt; &lt;option value="2028"&gt;2028&lt;/option&gt; &lt;option value="2029"&gt;2029&lt;/option&gt; &lt;option value="2030"&gt;2030&lt;/option&gt; &lt;/select&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload