1

I have the following code below, which is supposed to open a modal with an input box as soon as you start typing. I have made progress to the point where the modal opens, however I can't figure out how to focus on the actual input box (inside my link function in the directive). Any ideas would be appreciated. Also, if there are any other suggestions for improvements, please let me know.

Please see this plnkr http://plnkr.co/edit/ZMN56G

link: function(scope, elem, attrs){

  //CAN'T FIGURE THIS OUT
  var input = elem.children()[1]
  input.focus();
  console.log(input)      
}

And HTML....

<div class="modal-body">
   <input type="text" ng-model="selected.query"/>
      Selected: <b>{{ query}}</b>
</div>
user1142130
  • 1,617
  • 3
  • 20
  • 34
  • Does 'input' actually have a value or is it undefined? – Bas Slagter Nov 13 '15 at 15:39
  • if you look at the HTML, you will see an input field. essentially it's the input box that i'm trying to focus on – user1142130 Nov 13 '15 at 15:42
  • Yes, I believe that the input is there if you look at the html...but does the selector give you a reference to it at that point in your code? – Bas Slagter Nov 13 '15 at 15:43
  • Yes it does, if you see the console log statement, it returns the modal-body div...and inside of that is the input box. So, essentially, I can get the modal-body div, but I can't figure out how to get to the input inside of it. – user1142130 Nov 13 '15 at 15:45
  • Possible duplicate of [How to set focus on input field?](http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field) – vp_arth Nov 13 '15 at 15:46
  • http://plnkr.co/edit/6kHuj0?p=preview – vp_arth Nov 13 '15 at 15:50
  • What do you get when logging the var input? Also...adding a timeout like @vp_arth suggest will indeed probably fix your problem but I was trying to get to the essence here ;) – Bas Slagter Nov 13 '15 at 15:51
  • Your selecting the modal body div not the input. – ste2425 Nov 13 '15 at 15:54
  • `$timeout` need for render modal. Other approach is delegate this work to input directive. There are ton of variants in the linked question. – vp_arth Nov 13 '15 at 15:56
  • but i don't want to use another directive... i want to do all of the work in the same directive – user1142130 Nov 13 '15 at 16:06

0 Answers0