1
Good morning. I have the following code:
HTML:
<asp:CheckBox ID="ckPergunta" runat="server" onchange='SelectChoices(this);' />
Javascript:
<script type="text/javascript">
SelectChoices();
function SelectChoices() {
var opc = document.getElementById('<%=ckPergunta.ClientID%>').checked;
if (opc == false) {
document.getElementById('<%=dlTipoResposta.ClientID %>').style.display = "none";
document.getElementById('<%=txtRes.ClientID %>').style.display = "none";
}
else {
document.getElementById('<%=dlTipoResposta.ClientID %>').style.display = "block";
document.getElementById('<%=txtRes.ClientID %>').style.display = "block";
}
}
</script>
It happens that when I check, in the checkbox the Javascript code does not run. In other words, nothing happens. But if I put an onChange on an Asp:Textbox the Javascrip code works right. What’s wrong?
You in your
onchange
ofcheckbox
you are sending (trying) the element to the function when it has no input parameter. Even if you want to send the element, in your case thecheckbox
, you have to use$(this)
, and not justthis
. Now you need to see what you really want so we can help you... if you erase thethis
of youronchange
already works– CesarMiguel