php - JavaScript: Updating <select> list of days given month input -
my raw html has 3 <select>
inputs side-by-side so:
<select id="smonth" name="smonth" onchange="updatesdaysinput()"> <option value="month">month</option> <option value="1" <?php if ($smonth == '1') { echo 'selected'; } ?>>january</option> <option value="2" <?php if ($smonth == '2') { echo 'selected'; } ?>>february</option> <option value="3" <?php if ($smonth == '3') { echo 'selected'; } ?>>march</option> <option value="4" <?php if ($smonth == '4') { echo 'selected'; } ?>>april</option> <option value="5" <?php if ($smonth == '5') { echo 'selected'; } ?>>may</option> <option value="6" <?php if ($smonth == '6') { echo 'selected'; } ?>>june</option> <option value="7" <?php if ($smonth == '7') { echo 'selected'; } ?>>july</option> <option value="8" <?php if ($smonth == '8') { echo 'selected'; } ?>>august</option> <option value="9" <?php if ($smonth == '9') { echo 'selected'; } ?>>september</option> <option value="10" <?php if ($smonth == '10') { echo 'selected'; } ?>>october</option> <option value="11" <?php if ($smonth == '11') { echo 'selected'; } ?>>november</option> <option value="12" <?php if ($smonth == '12') { echo 'selected'; } ?>>december</option> </select> <select id="sday" name="sday"> <option value="day">day</option> </select> <select id="syear" name="syear" onchange="updatesdaysinput()"> <option value="year">year</option> <option value="1998" <?php if ($syear == '1998') { echo 'selected'; } ?>>1998</option> <option value="1999" <?php if ($syear == '1999') { echo 'selected'; } ?>>1999</option> <option value="2000" <?php if ($syear == '2000') { echo 'selected'; } ?>>2000</option> <option value="2001" <?php if ($syear == '2001') { echo 'selected'; } ?>>2001</option> <option value="2002" <?php if ($syear == '2002') { echo 'selected'; } ?>>2002</option> <option value="2003" <?php if ($syear == '2003') { echo 'selected'; } ?>>2003</option> <option value="2004" <?php if ($syear == '2004') { echo 'selected'; } ?>>2004</option> <option value="2005" <?php if ($syear == '2005') { echo 'selected'; } ?>>2005</option> <option value="2006" <?php if ($syear == '2006') { echo 'selected'; } ?>>2006</option> <option value="2007" <?php if ($syear == '2007') { echo 'selected'; } ?>>2007</option> <option value="2008" <?php if ($syear == '2008') { echo 'selected'; } ?>>2008</option> <option value="2009" <?php if ($syear == '2009') { echo 'selected'; } ?>>2009</option> <option value="2010" <?php if ($syear == '2010') { echo 'selected'; } ?>>2010</option> <option value="2011" <?php if ($syear == '2011') { echo 'selected'; } ?>>2011</option> <option value="2012" <?php if ($syear == '2012') { echo 'selected'; } ?>>2012</option> <option value="2013" <?php if ($syear == '2013') { echo 'selected'; } ?>>2013</option> <option value="2014" <?php if ($syear == '2014') { echo 'selected'; } ?>>2014</option> <option value="2015" <?php if ($syear == '2015') { echo 'selected'; } ?>>2015</option> </select>
this relevant javascript code:
syear = document.getelementbyid("syear"); smonth = document.getelementbyid("smonth"); sday = document.getelementbyid("sday"); function updatesdaysinput() { "use strict"; var monthdays = findsmonthdays(smonth.value), newoption = document.createelement("option"), curlength = sday.length; if (curlength > monthdays) { (i = curlength; > monthdays; = - 1) { sday.remove(i); } } if (curlength < monthdays) { (i = curlength; < monthdays; = + 1) { newoption.text = i; newoption.value = i.tostring(); sday.add(newoption); } } }
the code supposed add or remove options sday
appropriate given value of smonth
. instead adds single option bottom of sday
. know might issue?
move newoption = document.createelement("option")
inside loop create new element every loop iterations below
if (curlength < monthdays) { (i = curlength; < monthdays; = + 1) { newoption = document.createelement("option"); newoption.text = i; newoption.value = i.tostring(); sday.add(newoption); } }
hope helps.
Comments
Post a Comment