How to get the focused element with jQuery?

0 votes
Using jQuery, how can I get the input element that has the caret's (cursor's) focus?

Or in other words, how to determine if an input has the caret's focus?
Sep 20 in Java-Script by kartik
• 30,930 points
61 views

1 answer to this question.

0 votes

Hello @kartik,

// Get the focused element:
var $focused = $(':focus');

// No jQuery:
var focused = document.activeElement;

// Does the element have focus:
var hasFocus = $('foo').is(':focus');

// No jQuery:
elem === elem.ownerDocument.activeElement;

So you can use:

document.activeElement

And if you want a jQuery object wrapping the element:

$(document.activeElement)

Hope it helps!!
Thank you!!

answered Sep 20 by Niroj
• 67,020 points

Related Questions In Java-Script

0 votes
1 answer

How to get the current URL with JavaScript?

Hello @kartik, Use: window.location.href As noted in the comments, ...READ MORE

answered Aug 28 in Java-Script by Niroj
• 67,020 points
60 views
0 votes
1 answer

How to Change the selected value of a drop-down list with jQuery?

Hello @kartik, With hidden field you need to ...READ MORE

answered Sep 9 in Java-Script by Niroj
• 67,020 points
33 views
0 votes
1 answer

How to get the anchor from the URL using jQuery?

Hello @kartik, For current window, you can use this: var ...READ MORE

answered Oct 9 in Java-Script by Niroj
• 67,020 points
27 views
0 votes
1 answer

How to set outerHTML with jQuery?

hello @kartik, This is innterHTML because it's inside ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 67,020 points
107 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 67,020 points
2,797 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27 in PHP by Niroj
• 67,020 points
460 views
0 votes
1 answer

How can we create a session in PHP?

Hello @kartik, session_start() function is used in PHP to ...READ MORE

answered Mar 27 in PHP by Niroj
• 67,020 points
89 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27 in PHP by Niroj
• 67,020 points
133 views
0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18 in Java-Script by Niroj
• 67,020 points
2,665 views
0 votes
1 answer

How to get the jQuery $.ajax error response text?

Hello @kartik, Try: error: function(xhr, status, error) { ...READ MORE

answered Jul 27 in Java-Script by Niroj
• 67,020 points
349 views