Home > Blockchain >  Change value input onclick witch setTimeout
Change value input onclick witch setTimeout

Time:06-14

How can I remove special symbols (R$) when clicking the button, or simply remove if it exists?

setTimeout(function clean() {
var input = document.getElementById('value')
input.value = input.value.replace(/[!$(){}[\]:;R< ?\\>]/g,'')
},3000)

I would like that when clicking on the button, it would also remove the symbol R$

I have so far

setTimeout(function clean() {
var input = document.getElementById('value')
onclick = input.value = input.value.replace(/[!$(){}[\]:;R< ?\\>]/g,'')
}, 2000)
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button  onclick="clean()">clean</button>

CodePudding user response:

Just use the String.replace() method and then the String.trim() method to ensure there are no leading or trailing spaces left behind..

const input = document.getElementById("value");
document.querySelector("button").addEventListener("click", function(){
  input.value = input.value.replace("R$","").trim();
});
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button>clean</button>

CodePudding user response:

Your regex for removing the special symbol is right, only error was of setTimeout & function declaration error.

I have fixed that code error.

Below snippet will work, but in function clean() it will wait for 1000ms because of setTimeout function.

function clean() {
  setTimeout(()=>{
    var input = document.getElementById('value')
    onclick = input.value = input.value.replace(/[!$(){}[\]:;R< ?\\>]/g,'');
    input.value = input.value.trim();
  }, 1000) // code inside setTimeout function will wait 1000ms to execute
}
<input id="value" name="item_valor" value="R$ 14,99" >
remover simbolo pagseguro
<br>

<button  onclick="clean()">clean</button>

  • Related