0

I have the following code for selecting all text inside input field:

<input id="userName" class="form-control" type="text" name="enteredUserName" data-ng-show="vm.userNameDisplayed()" data-ng-model="vm.enteredUserName"> 

and method to select all text:

vm.userNameDisplayed = function() { var textArea = angular.element('#userName'); textArea.setSelectionRange(0, vm.enteredUserName.length) } 

but after calling this method nothing is selected.

5
  • stackoverflow.com/questions/4067469/… Commented Aug 1, 2017 at 8:49
  • 1. I don't need to do it on onClick method 2. I have the same code as in provided example Commented Aug 1, 2017 at 8:51
  • When do you expect the text to be selected? As soon as user types? Commented Aug 1, 2017 at 8:54
  • Secondly, ng-show expects an expression that resolves to truthy/falsy. In your case, the function userNameDisplayed will be called when the page is rendered. If there is no data at that time, it will not select anything. Commented Aug 1, 2017 at 8:56
  • Searching on the title returns 5,000 results. Commented Aug 1, 2017 at 9:14

1 Answer 1

1

Try focusing the textArea first using textArea.focus();

vm.userNameDisplayed = function() { var textArea = angular.element('#userName'); textArea.focus(); textArea.setSelectionRange(0, vm.enteredUserName.length) } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.