0

I have a web form with several fields. Three of them are ano_evento, mes_evento and dia_evento (year, month and day). I want to get the values from these fields, convert it to a date format and show the formatted date in another field called fecha_evento. The user sets a value for ano_evento (e.g. 2014), a value for mes_evento (e.g. 11) and a value for dia_evento (e.g. 25). The value for fecha_evento should then be 2014-11-25. All three fields have an onchange method in it:

For ano_evento:

<select name="ano_evento" id ="ano_evento" onchange="cambiar_fecha()">

For mes_evento:

<select name="mes_evento" id ="mes_evento" onchange="cambiar_fecha()">

For dia_evento:

<select name="dia_evento" id="dia_evento" onchange ="cambiar_fecha()">

The field for fecha_evento:

<input type="text" name="fecha_evento" id="fecha_evento" value="0" size="32" />

And this is the script:

<script>
function cambiar_fecha(){
alert (document.getElementById("fecha_evento").value);

var ano = document.getElementById("ano_evento").value;

var mes = document.getElementById("mes_evento").value; // 

var dia = document.getElementById("dia_evento").value;

document.getElementById("fecha_evento").value = ano+"-"+mes+"-"+dia;
}

</script>

When the user changes any of the three fields values, the alert inside the script is shown, but the value from the field fecha_evento doesn't change.

Any help is welcome

6
  • If you console.log(ano, mes, dia) inside the cambiar_fecha function, what is the output? Commented Dec 19, 2014 at 23:25
  • @ateich, I will check it now and inform you... Commented Dec 19, 2014 at 23:25
  • 1
    It seems to work for me. fiddle. I only removed a quote to avoid a syntax error. Commented Dec 19, 2014 at 23:29
  • @ateich, as expected the log is 2014 11 25 Commented Dec 19, 2014 at 23:30
  • 1
    Please fix the "dia copying error, you're going to get lots of incorrect answers because of that. Commented Dec 19, 2014 at 23:33

2 Answers 2

1

how are you?

Try this code:

<select name="ano_evento" id="ano_evento">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<select name="mes_evento" id="mes_evento">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<select name="dia_evento" id="dia_evento">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<input type="text" name="fecha_evento" id="fecha_evento">

<script>

    var ano = document.getElementById("ano_evento");
    var mes = document.getElementById("mes_evento");
    var dia = document.getElementById("dia_evento");
    var fecha = document.getElementById("fecha_evento");

    function cambiar_fecha() {
        alert(fecha.value);
        fecha.value = ano.value + "-" + mes.value + "-" + dia.value;
        alert(fecha.value);
    }

    ano.onchange = cambiar_fecha;
    mes.onchange = cambiar_fecha;
    dia.onchange = cambiar_fecha;

</script>

Recommendations: - Save the elements in variables to optimize execution avoiding constantly DOM access. - Set events handler from JS. - Use browser console to see if there are JS errors.

Regards.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of the inline javascript, I would just go with a querySelectorAll

DEMO

HTML:

<select name="ano_evento" id ="ano_evento" >
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<select name="mes_evento" id ="mes_evento" >
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<select name="dia_evento" id="dia_evento" >
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<input type="text" name="fecha_evento" id="fecha_evento" value="0" size="32" />

Javascript:

var a = document.querySelectorAll('select');
console.log(a);
for(var i = 0; i<a.length;i++) {
    a[i].addEventListener('change',cambiar_fecha);
}
function cambiar_fecha(){

alert (document.getElementById("fecha_evento").value);

var ano = document.getElementById("ano_evento").value;

var mes = document.getElementById("mes_evento").value; // 

var dia = document.getElementById("dia_evento").value;

document.getElementById("fecha_evento").value = ano+"-"+mes+"-"+dia;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.