-1

I am back. How am I suppose to change a select content if I choose an option of other select. This is my JSFiddle. What I want is that if I select the 'mes' = 2 option (february). It shows me only 28 days on the 'dia' select (days). I don't know how to refer when the number 2 is selected so I can build a if structure. Thx in advance, my english is not the best... (I don't wanna use JQuery)

This is my whole HTML:

<p>Dia
    <select name="dia" id="dia"></select>Mes
    <select name="mes1" id="mes"></select>Año
    <select name="año" id="año"></select>
</p>
<p>
    <input type="text" name="txtFecha1" id="txtFecha1" disabled/>
    <input type="submit" name="capturar" id="capturar" value="Capturar" onclick="capturar()" />
</p>
<p>
    <input type="text" name="txtFecha2" id="txtFecha2" disabled/>
    <input type="submit" name="capturar2" id="capturar2" value="Capturar" onclick="capturar()" />
</p>
<p>
    <input type="submit" name="diferencia" id="diferencia" value="Diferencia" onclick="diferencia()" />
</p>
<p id="pParrafo">Dias entre las fechas:</p>

onClick functions are not programmed yet.

This is the JS:

var mes = new Array();
for (i = 1; i <= 12; i++) {
    mes[i] = i;
}


var seleccionar = document.getElementById('mes');


for (var i = 1; i < mes.length; i++) {
    var option = document.createElement('option');
    option.innerHTML = mes[i];
    option.value = mes[i];
    seleccionar.appendChild(option);
}


var año = new Array();
for (i = 1950; i <= 2014; i++) {
    año[i] = i;
}


var seleccionar = document.getElementById('año');


for (var i = 1950; i < año.length; i++) {
    var option = document.createElement('option');
    option.innerHTML = año[i];
    option.value = año[i];
    seleccionar.appendChild(option);
}
Veslor
  • 80
  • 1
  • 11
  • You mean like `document.getElementById('mes').value`? – Moss Oct 21 '14 at 06:48
  • @Moss By doing that can I use`document.getElementById('mes').value == 2` to refer to february, I am new to programming sorry. – Veslor Oct 21 '14 at 06:51
  • Yep, it is that simple. You should be able to just test that quickly and see for yourself. – Moss Oct 21 '14 at 06:52
  • @I'll do it, thx. Will come again if any problem ;) – Veslor Oct 21 '14 at 06:54
  • Probably much more efficient to create 31 options, then remove/restore the last 4 depending on the year and month. Options can be moved to a hidden select, or just assigned to a document fragment. You can also do `seleccionar.appendChild(new Option(text, value))`. – RobG Oct 21 '14 at 06:59
  • @moss It doesn't do what I want as i though, http://jsfiddle.net/pe2opbut/1/ Check out the code, when i choose 2, it doesnt change days select on the fly. Thats what i wanna do :S – Veslor Oct 21 '14 at 07:00
  • @RobG I dont see the way of using `seleccionar.appendChild(new Option(text, value))`to change the number of days when selected an specific month. Check out my code jsfiddle.net/pe2opbut/1 – Veslor Oct 21 '14 at 07:08
  • For manipulating the DOM it is wise to learn jQuery. – Moss Oct 21 '14 at 17:28
  • @Moss I am learning JS at uni, we will start using JQuery in a few weeks. The professor wants us to use JS only. – Veslor Oct 21 '14 at 20:29

2 Answers2

2

Here's a simple example of how I'd do it: add and remove options from the days select based on the year and month selected. It doesn't create or destroy options, it just moves them around.

It's less code and more efficient than alternatives.

<script>
// Simple function to return days in month given year and month
// Month is calendar number, e.g. 1 = jan, 2 = feb, etc.
function daysInMonth(year, month) {
  return new Date(year, month, 0).getDate();
}

// Update the number of options in the day select    
var updateDay = (function() {

  // Document fragment to store spare options
  var frag = document.createDocumentFragment();

  return function(control) {

    // Get a reference to the form and day select
    var form = control.form;
    var daySelect = form.day;

    // Get days in month based on year and month selected
    var days = daysInMonth(form.year.value, form.month.value);

    // Remove or restore days to correct number
    while(daySelect.options.length != days) {
      daySelect.options.length > days? frag.appendChild(daySelect.lastChild) :
                                       daySelect.appendChild(frag.lastChild);
    }
  }
}());
</script>

<!-- Simple date selector -->
<form>
Year: <select name="year" onchange="updateDay(this)">
  <option value="2010">2010
  <option value="2011">2011
  <option value="2012">2012
  <option value="2013">2013
  <option value="2014">2014
</select>

Month: <select name="month" onchange="updateDay(this)">
  <option value="1">Jan
  <option value="2">Feb
  <option value="3">Mar
  <option value="4">Apr
  <option value="5">May
  <option value="6">Jun
  <option value="7">Jul
  <option value="8">Aug
  <option value="9">Sep
  <option value="10">Oct
  <option value="11">Nov
  <option value="12">Dec
</select>

Day: <select name="day">
  <script>
    // Saves on markup...
    for (var i=1; i<32; i++) {
      document.write('<option value="' + i + '">' + i + '<\/option>');
      console.log(i);
    }
  </script>
</select>
</form>
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thank you for your answer. Appreciate it.This is what I've done at the end. http://jsfiddle.net/vv4ghof1/1/ – Veslor Oct 21 '14 at 20:24
  • Can you explain me what `daySelect.options.length > days? frag.appendChild(daySelect.lastChild) : daySelect.appendChild(frag.lastChild);` does? I am new to that kind of expression – Veslor Oct 21 '14 at 20:47
  • It's called the [*conditional operator*](http://ecma-international.org/ecma-262/5.1/#sec-11.12), there's a question about it [*here*](http://stackoverflow.com/questions/21087590/how-to-use-the-conditional-operator). I've used it to determine whether to remove or replace options to make the right number in the *day* select. – RobG Oct 21 '14 at 21:19
1

youcan set filldia function on change of month select element try this code :

HTML :

<select name="mes1" id="mes" onchange="fillDia(this)"

Javascript

// generate dia items
var dia = new Array();

fillDia(document.getElementById('mes')); 

function fillDia(opt) {
    // empty dia select element options
    document.getElementById('dia').options.length = 0;

    var maxDays = 31;
    //alert (opt.value);
    if (opt.value == '2') maxDays = 28; //feburary
    if (opt.value == '4' || opt.value == '6' || opt.value == '9' || opt.value == '11') maxDays = 30; //april , june , september , november

    for (i = 1; i <= maxDays; i++) {
        dia[i] = i;
    }
    //se guarda el select dia
    var dias = document.getElementById('dia');

    //generar select para dia
    for (var i = 1; i <= maxDays; i++) {
    var option = document.createElement('option');
    option.innerHTML = dia[i];
    option.value = dia[i];
        dias.appendChild(option);
    }

}     

JSFIDDLE DEMO

Farshad
  • 1,465
  • 1
  • 9
  • 12
  • Thank you! But the code adds the days from the last month, I explain myself. If you select "2" for example and then "3", it shows you 28 days following by another 31. And I cant select the first month. I will try to find out the problem, but that surely is the way to go, thank you. – Veslor Oct 21 '14 at 07:22
  • by this solution you can get month value on each change select action .please clarify your scenario for filling dia ? – Farshad Oct 21 '14 at 07:28
  • When I select "2" on month select. It shows me 1,2,3..30,31 and then 1,2,3...27,28. It doesnt delete the previous selected month. If I click another month it will show me that month next to the last one I select. What i want is to show the days of the month selected. Thank you for your time. – Veslor Oct 21 '14 at 07:34
  • aha ok , you can simply add `document.getElementById('dia').options.length = 0;` right after `function fillDia(opt) {`. I have Updated my answer – Farshad Oct 21 '14 at 07:40
  • You are amazing! Thank you, I am learning so much from you guys ;) – Veslor Oct 21 '14 at 07:42
  • what do you mean saying "no at all" =( My english is not the best :P – Veslor Oct 21 '14 at 07:50
  • used as a polite reply after someone has thanked you: "Thanks for helping." , "Not at all." – Farshad Oct 21 '14 at 07:56