I have a page that displays some user information. One of the fields is the telephone field. I want to apply a mask in this field when loading the page. I can even put the mask on, but only on the onKeyUp event. The problem is that the mask only appears when I press any key, and I need the mask to be applied as soon as I load the page.
Follow JS.
<!-- Script para máscara de telefone -->
<script type="text/javascript">
/* Máscaras ER */
function mascara( o, f ){
v_obj = o
v_fun = f
setTimeout( "execmascara()", 1 )
}
function execmascara(){
v_obj.value = v_fun( v_obj.value )
}
function mtel( v ){
v = v.replace( /\D/g, "" ); // Remove tudo o que não é dígito
v = v.replace( /^(\d{2})(\d)/g, "($1) $2" ); // Coloca parênteses em volta dos dois primeiros dígitos
v = v.replace( /(\d)(\d{4})$/, "$1-$2" ); // Coloca hífen entre o quarto e o quinto dígitos
return v;
}
function id( el ){
return document.getElementById( el );
}
window.onload = function(){
id( 'txt-telefone' ).onkeyup = function(){
mascara( this, mtel );
}
}
</script>
How do I make the mask appear when loading the page?