Problem with removeAllRanges()

Asked

Viewed 26 times

0

I am developing a Mask in javascript for an input and am having a problem with the function selection.removeAllRanges(); every time I use it, it removes the input cursor and I can’t use the Backspace key to delete the values contained in the input.

function fdecimal(vobj,vint,vdec)

{ var Selection = window.getSelection(); Selection.removeAllRanges();

document.getElementById(vobj.id).select();

vlista = "-0123456789";

vchar  = String.fromCharCode(event.which);

/*=== Caracter digitado deve estar parametrizado na lista (vlista) ===*/
if (vlista.indexOf(vchar,0) == -1)
    return event.keyCode = 0;
vaux = vobj.value + vchar;
vaux = vaux.replace(",","");
/*=== tratamento sinal negativo ===*/
vneg = vaux.indexOf("-",0);
while (vaux.indexOf("-") != -1)
{
    vaux = vaux.replace("-","");
}
/*=== Tamanho máximo numero ===*/
if(vaux.length > (vint + vdec)) {
    return event.keyCode = 0;
}
/*=== Completa zeros a frente ===*/
vaux = "" + Number(vaux);
for (i=vaux.length;i<=vdec;i++)
{
   vaux = '0' + vaux;
}
/*=== Coloca virgula como separador de decimal ===*/
vret = vaux.substr(0,vaux.length-vdec) + ',' + vaux.substr(vaux.length-vdec,vdec);
vobj.value = (vneg!=-1?'-':'') + vret;
selection.removeAllRanges();    

}

  • Try adding a new empty range at the end after removing.

  • @bfavaretto without success.. the funny thing is that when I minimize the browser and reopen, the cursor reappears, the same happens when some Alert is called and then closed.

  • and simply .focus() in the input?

  • I’ve tried to do it too and it doesn’t work.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.