wongadob Posted July 27, 2012 Share Posted July 27, 2012 Is there any way in jQuery or Javascript that you can detect if the current focused/activce element is a textarea or input area. The reason for this is I need to use hotkeys but they will be standard keys (i.e. not control+key) so I need to do a key listener and then make it ignore the keypress and fall through if the user is inputting text. I can do this by testing the ID for the ID's of all mt inputs/textareas, but that seems to be a tad long winded. Be great if there was something like $(document).currentactiveID But can't seem to find anything As always any help much appreciated Link to comment Share on other sites More sharing options...
eTianbun Posted July 27, 2012 Share Posted July 27, 2012 document.getElementById('formElement').type Link to comment Share on other sites More sharing options...
wongadob Posted July 27, 2012 Author Share Posted July 27, 2012 Thanks that worked fine once i got the Id. I had to find the Id first as the listener was on the body element Link to comment Share on other sites More sharing options...
ShadowMage Posted July 27, 2012 Share Posted July 27, 2012 You could use jQuery's :focus selector and then check the tag name: if (($(':focus').attr('tagName') == 'textarea') || ($(':focus').attr('tagName') == 'input')) { //do something} Link to comment Share on other sites More sharing options...
dsonesuk Posted July 27, 2012 Share Posted July 27, 2012 $("input[type='text'], textarea").focus( function(){alert("you have focused on a textarea or input of type text")}); To target all inputs remove '[type=text]', to target specific ID or class add to input selector input.myclass, input#myid Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now