0
  element.on("click", function(){
    element[0].querySelector("input").focus()
    element[0].querySelector("input").on('keypress',function(key){
        console.log(key)
    }
  })

This returns this error:

Uncaught TypeError: undefined is not a function 
runner:18(anonymous function) 
runner:18(anonymous function) 
angular.js:2843forEach 
angular.js:325eventHandler

And the JSBin

Thanks!

Mohamed El Mahallawy
  • 13,024
  • 13
  • 52
  • 84

1 Answers1

3

querySelector returns a DOM object. The .on function is a jQuery function. You need to wrap it:

$(element[0].querySelector("input")).on('keypress',function(key){
    console.log(key)
}

Also, I would be weary of adding event handlers inside of other event handlers. Whenever you click the element, the keypress event will be added. If you click the element 5 times, the input will now have 5 active keypress events unless you're removing it at some point. You should only do it if you're aware of these implications - and even then it can make code hard to follow.

Stryner
  • 7,288
  • 3
  • 18
  • 18