Thiago Vinic
Thiago Vinic

Reputation: 15

Change value input onclick witch setTimeout

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 class="" onclick="clean()">clean</button>

Upvotes: 0

Views: 66

Answers (2)

Aniket Gawas
Aniket Gawas

Reputation: 184

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 class="" onclick="clean()">clean</button>

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65808

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>

Upvotes: 1

Related Questions