0
I have a function, that when clicking on the dropdownlist, it checks and changes the mask of a field:
function HideTextBox(ddlId) {
var ControlName = document.getElementById(ddlId.id);
if (ControlName.value == 'P') //it depends on which value Selection do u want to hide or show your textbox
{
document.getElementById('txtCpf').style.display = '';
$("#txtCpf").mask("999.999.999-99");
} else {
document.getElementById('txtCpf').style.display = '';
$("#txtCpf").mask("99.999.999/9999-99");
}
}
This is my html:
<div class="grid-1-3 margin-txt">
<asp:Label ID="Label5" runat="server" Text="Tipo de Pessoa:"></asp:Label>
<asp:DropDownList ID="ddlIDType" runat="server" onchange="HideTextBox(this);" CssClass="form-control">
<asp:ListItem Value="P">Física</asp:ListItem>
<asp:ListItem Value="L">Jurídica</asp:ListItem>
</asp:DropDownList>
</div>
<div class="grid-1-3 margin-txt">
<asp:Label ID="Label4" runat="server" Text="CPF ou CNPJ"></asp:Label>
<asp:TextBox ID="txtCpf" runat="server" CssClass="form-control" placeholder="CPF ou CNPJ" required="required"></asp:TextBox>
</div>
But when I do not select the field, it does not load the mask, how can I call the function in the form load, passing the field "ddlIDType"? I tried to pass like this, but it returns me error:
ScriptManager.RegisterClientScriptBlock(this, GetType(), "", "HideTextBox(" + ddlIDType.SelectedValue + ");", true);
Error that is returning me when I load the function this way in the load:
Uncaught Referenceerror: Hidetextbox is not defined
This way it already works, which is by clicking on the ddlidtype it changes the mask, but I need for when it loads the form, that it comes with the mask filled with physical person, which is what is not happening.
– Mariana
So I put the call
$("#txtCpf").mask("999.999.999-99");
outside the event, as the first instruction to be executed when the page is loaded.– Pablo Tondolo de Vargas
But when the field already comes filled with CNPJ, it always loads the cpf mask, it only changes to CNPJ if I change in the dropdownlist.
– Mariana
I encapsulated the mask logic in a Function, see if it helps you now
– Pablo Tondolo de Vargas
So it was only on the CNPJ mask, and it has q be loaded according to the dropdownlist, so I wanted q it called in the form load, because if the txtid had empty, it called the CPF mask function, otherwise it would load the CNPJ.
– Mariana
Actually I missed taking the value of the element, but I already edited the answer
– Pablo Tondolo de Vargas
Now yes Pablo rs, thank you so much for the help, it worked perfectly.
– Mariana