How to select field content when receiving Focus?

Asked

Viewed 715 times

1

I have a problem that seems to be simple, but it has taken me a long time looking for possible solutions and so far I have not been able to solve.

I need to select the content of an Input in an HTML form with Bootstrap when receiving Focus.

By default HTML5 already selects the field content when receiving Focus, but I am using an Angularjs( ng-currency ) directive to format decimals, and after adding the directive no longer selects.

In short...

What I want ?

Well an image is worth more than a thousand words, when receiving Focus(tab for example) I want the content of the input to be all selected on the screen:

inserir a descrição da imagem aqui

What I’ve tried to do?

I was able to do it in the click event inside the field, like this:

<input type="text"
   class="col-sm-12 form-control"
   ng-model="campo"
   ng-currency currency-symbol=""
   ng-click="selectAllClick($event)">

$scope.selectAllClick = function ($event) {
   $event.target.select();
}

But accurate in the event of Focus, I have tried to use ng-Focus to call this function, but it does not work.

The directive I’m using is this: https://github.com/aguirrel/ng-currency

The problem is this:

inserir a descrição da imagem aqui

Does anyone have any idea?

  • Have you tried onclick="this.select()" ?

  • I’ve tried yes, onclick works, but onfocus doesn’t.

  • I tried to use so, I saw here in the forum: onfocus="this.select();" onmouseup="Return false;", but also did not solve

  • in the test I did here using that same code worked normal, even using the tab, weird, I’ll try something else here

  • So, for me it only works if I click inside the field, or if I take ng-currency out of the input, then it works right.

  • Well, the only thing that changes is that I have this input inside a table, but even if I test outside of it, it doesn’t work either.

  • You want to select while hovering over?

  • No, entering the field, via tab is what I need.

  • I updated the image of how it currently happens, when entering the field it plays the | to the last element, and I want it to come selected.

Show 5 more comments

1 answer

0

<input type="text" class="col-sm-12 form-control"ng-model="campo"ng-currency currency-symbol="" onClick="this.focus();this.select()"/>

try so it goes right, I’ve spent a long time trying to find the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.