Click here to Skip to main content
15,886,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
if (document.getElementById('TextBox1').focus()== true)
// want to check focus fo text box here ..??
// not checking if condition.
Posted

1 solution

Question not written properly, but looks like you want to find the control in focus at a given time.

You can try either of these:
1. Use document.activeElement property in JS to know the currently focused element. Use it accordingly.
2. Use jQuery selector :focus, example:
JavaScript
var element = document.querySelector(":focus");
 
Share this answer
 
Comments
_Dhull 14-May-12 5:44am    
function abc()
{
if (document.getElementById('TextBox1').focus())
{
// write in textbox1
}
else
{
// write in text box2
}
}
++++++++++++++++++++++++++++++++++

1> i have a keyboard in javascript for login window.
2> i have two text boxes (a) login_name (b)Password
whenever i try to write with keyboard on page ....... it writes only in login_name text box

my else part is not working ..
+++++++++++++++++++++++++++++++++++
i am calling function abc() on every button of keyboard on page.
Sandeep Mewara 14-May-12 8:26am    
I guess, document.getElementById('TextBox1').focus() will not work as TextBox is a server control and you cannot access it directly using it's normal ID.

Try:
<%=TextBox1.ClientID %>
_Dhull 15-May-12 8:00am    
Thanks for reply :-

Not working :-(
Sandeep Mewara 15-May-12 11:32am    
Do a View Source of your page, find the textbox ID you want to focus and use that ID here.
_Dhull 18-May-12 6:28am    
wow..!!
its working ..:-)
its working ..:-)
its working ..:-)
its working ..:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900