1

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')
}
Lars Beck
  • 3,446
  • 2
  • 22
  • 23

1 Answers1

1

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')
}
Soolie
  • 1,812
  • 9
  • 21