In my Angular view I have an input field and some clickable goodies using ng-click.
Is there an angular way or simple vanilla js way to prevent the input field from LOSING focus (if it was in focus) when one of these ng-clicks is clicked?
<input type="text" class="form-control" ng-model="foo">
<button ng-click="someThing()">click me</button>
Not with ng-click
, because by the time that fires, the clicked element has already taken the focus from the input.
But ng-mousedown
should work, if you $event.preventDefault()
in your event handler. Here's a demonstration in vanilla JS; the same should work in Angular:
var preventBlur = function(event) {
console.log("click");
event.preventDefault();
}
<input type='text' onblur="alert('blurred')">
<button onmousedown="preventBlur(event)">Click</button>
If you click the button while the input field is in focus, the mousedown will trigger without causing the input field to lose focus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With