Using a Web form with Master Page im getting null from a javascript sentence:
Javascript:
//becouse I was getting error with this line I change the trigger to the onclick attribute on the button tag, but keep getting the same error
//document.getElementById("btn_fecha").onclick = function() {validaFecha()};
function validaFecha(){
var in_dia = document.getElementById("<%=drp_dia.ClientID%>");
var in_mes = document.getElementById("<%=drp_mes.ClientID%>");
var in_agno = document.getElementById("<%=drp_agno.ClientID%>");
var dia = in_dia.options[in_dia.selectedIndex].value;
var mes = in_mes.options[in_mes.selectedIndex].value;
var agno = in_agno.options[in_agno.selectedIndex].value;
var dia_f = (dia < 10) ? "0" + dia : "" + dia;
var mes_f = (mes < 10) ? "0" + mes : "" + mes;
var fecha = new Date(agno + "-" + mes_f + "-" + dia_f)
var fecha_f = formatDate(fecha);
document.getElementById("txt_fecha_nacimiento").value = fecha_f;
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
and this are the tags Im having problem to get:
<td>
<asp:DropDownList ID="drp_dia" runat="server" ClientIDMode="static"></asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="drp_mes" runat="server"></asp:DropDownList>
</td
<td>
<asp:DropDownList ID="drp_agno" runat="server"></asp:DropDownList>
</td>
<td>
<button Class="btn btn-secondary" ID="btn_fecha" type="button" onclick="validaFecha()">GUARDAR</button>
</td>
This is the error Im getting: enter image description here
I changed all the " in my Javascript to simple ' and put ClientIDMode="static" in all my asp tags (the ones I wanted to get), and only use the id in the javascript get by element not the "<%=ID.ClientID%>":
function validaFecha(){
var in_dia = document.getElementById('drp_dia');
var in_mes = document.getElementById('drp_mes');
var in_agno = document.getElementById('drp_agno');
var dia = in_dia.options[in_dia.selectedIndex].value;
var mes = in_mes.options[in_mes.selectedIndex].value;
var agno = in_agno.options[in_agno.selectedIndex].value;
var dia_f = (dia < 10) ? "0" + dia : "" + dia;
var mes_f = (mes < 10) ? "0" + mes : "" + mes;
var fecha = new Date(agno + "-" + mes_f + "-" + dia_f)
var fecha_f = formatDate(fecha);
document.getElementById("txt_fecha_nacimiento").value = fecha_f;
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
it started to work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.