Clearing an input on comma press doesn't clear the comma

Clash Royale CLAN TAG#URR8PPP up vote
19
down vote
favorite
I'm trying to clear an input when a user presses comma (,). So what I did was, on (keypress) I would check the keyCode and if it's a comma I would clear the input by setting the input value to input.value = ''.
The problem though is that even if it clears the letters I type in, it doesn't clear the comma. What am I doing wrong?
This is a fiddle of the problem.
Steps:
- Type in
abc. - Type
, - Observe that
abcwas cleared but the input still has a,.
HTML:
<input type="text" #elem (keypress)="clearInput($event)">
Code:
@ViewChild( 'elem' ) public element;
clearInput( e: KeyboardEvent )
if ( e.keyCode === 44 )
this.element.nativeElement.value = '';
else
console.log('Not a comma');
I'm using Angular 6.
javascript angular
add a comment |Â
up vote
19
down vote
favorite
I'm trying to clear an input when a user presses comma (,). So what I did was, on (keypress) I would check the keyCode and if it's a comma I would clear the input by setting the input value to input.value = ''.
The problem though is that even if it clears the letters I type in, it doesn't clear the comma. What am I doing wrong?
This is a fiddle of the problem.
Steps:
- Type in
abc. - Type
, - Observe that
abcwas cleared but the input still has a,.
HTML:
<input type="text" #elem (keypress)="clearInput($event)">
Code:
@ViewChild( 'elem' ) public element;
clearInput( e: KeyboardEvent )
if ( e.keyCode === 44 )
this.element.nativeElement.value = '';
else
console.log('Not a comma');
I'm using Angular 6.
javascript angular
add a comment |Â
up vote
19
down vote
favorite
up vote
19
down vote
favorite
I'm trying to clear an input when a user presses comma (,). So what I did was, on (keypress) I would check the keyCode and if it's a comma I would clear the input by setting the input value to input.value = ''.
The problem though is that even if it clears the letters I type in, it doesn't clear the comma. What am I doing wrong?
This is a fiddle of the problem.
Steps:
- Type in
abc. - Type
, - Observe that
abcwas cleared but the input still has a,.
HTML:
<input type="text" #elem (keypress)="clearInput($event)">
Code:
@ViewChild( 'elem' ) public element;
clearInput( e: KeyboardEvent )
if ( e.keyCode === 44 )
this.element.nativeElement.value = '';
else
console.log('Not a comma');
I'm using Angular 6.
javascript angular
I'm trying to clear an input when a user presses comma (,). So what I did was, on (keypress) I would check the keyCode and if it's a comma I would clear the input by setting the input value to input.value = ''.
The problem though is that even if it clears the letters I type in, it doesn't clear the comma. What am I doing wrong?
This is a fiddle of the problem.
Steps:
- Type in
abc. - Type
, - Observe that
abcwas cleared but the input still has a,.
HTML:
<input type="text" #elem (keypress)="clearInput($event)">
Code:
@ViewChild( 'elem' ) public element;
clearInput( e: KeyboardEvent )
if ( e.keyCode === 44 )
this.element.nativeElement.value = '';
else
console.log('Not a comma');
I'm using Angular 6.
javascript angular
javascript angular
edited Aug 8 at 12:30
Vadim Kotov
4,15353147
4,15353147
asked Aug 8 at 10:46
user3607282
95321231
95321231
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
Add 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');
Read more about preventDefault here.
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
add a comment |Â
up vote
7
down vote
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/
add a comment |Â
up vote
0
down vote
You need to do 2 minor changes:
- Use
keyupevent to get the latest key typed and include in the value ofinput - Use
e.key === ','in theifcondition
Here is the working JSFIDDLE
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
Add 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');
Read more about preventDefault here.
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
add a comment |Â
up vote
11
down vote
accepted
Add 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');
Read more about preventDefault here.
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
Add 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');
Read more about preventDefault here.
Add 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');
Read more about preventDefault here.
edited Aug 9 at 12:44
answered Aug 8 at 10:54
UtkarshPramodGupta
1,5731825
1,5731825
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
add a comment |Â
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
6
6
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
You could add some explanation: The insertion of the comma will be triggered by the same "keyup" event as your code. But the Browser has to run your code first, since your listener is not passive (it might preventDefault() or return false) - so the keypress event will run your listener first (clear the field) and then the default listener (inserting a comma into the empty field). So other solutions would be keyUp or marking your listener passive. - But preventDefault() is most idiomatic for your case.
â Falco
Aug 8 at 14:12
add a comment |Â
up vote
7
down vote
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/
add a comment |Â
up vote
7
down vote
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/
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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/
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/
answered Aug 8 at 10:48
Luca Kiebel
7,28931431
7,28931431
add a comment |Â
add a comment |Â
up vote
0
down vote
You need to do 2 minor changes:
- Use
keyupevent to get the latest key typed and include in the value ofinput - Use
e.key === ','in theifcondition
Here is the working JSFIDDLE
add a comment |Â
up vote
0
down vote
You need to do 2 minor changes:
- Use
keyupevent to get the latest key typed and include in the value ofinput - Use
e.key === ','in theifcondition
Here is the working JSFIDDLE
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You need to do 2 minor changes:
- Use
keyupevent to get the latest key typed and include in the value ofinput - Use
e.key === ','in theifcondition
Here is the working JSFIDDLE
You need to do 2 minor changes:
- Use
keyupevent to get the latest key typed and include in the value ofinput - Use
e.key === ','in theifcondition
Here is the working JSFIDDLE
answered Aug 8 at 10:50
Ankit Agarwal
21.7k41841
21.7k41841
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51744747%2fclearing-an-input-on-comma-press-doesnt-clear-the-comma%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password