Why String.replace() doesn't work with events?

61 views Asked by At

Why String.replace() doesn't work with events??

var el = document.querySelector('input')
var pat = /(\d{3})(\d{3})(\d{4})/;

el.oninput = function(){
  this.value.replace(pat, '$1-$2-$3')
}
1

There are 1 answers

3
Soolie On BEST ANSWER

Okay you aren't assigning it, or returning back, as you need to set the new value:

var el = document.querySelector('input')
var pat = /(\d{3})(\d{3})(\d{4})/;

el.oninput = function(){
  this.value = this.value.replace(pat, '$1-$2-$3')
}