How to clear an input value on some button press?
Use Event.preventDefault().
Add preventDefault()
in your clearInput
code as shown below:
clearInput (e: KeyboardEvent) {
if (e.keyCode === 44) {
e.preventDefault(); // <-- Here
this.element.nativeElement.value = '';
} else {
console.log('Not a comma');
}
}
Simply return false
if a comma is pressed:
class HomeComponent {
@ViewChild('elem') public element;
clearInput(e: KeyboardEvent) {
if (e.keyCode === 44) {
this.element.nativeElement.value = '';
return false;
} else {
console.log('Not a comma');
}
}
}
JSFiddle: https://jsfiddle.net/lucakiebel/zrehcwfy/1/