Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi everyone!

I am trying to detect whether or not a textbox has focus or has a mouse click. This is what I have right now.

JavaScript
if($("textbox").is(":focus"))
{
     $(#div).fadeOut(2000);
}


Currently I'm not seeing any changes nor getting any errors. Can someone show me what I'm doing wrong or how to best detect mouse clicks for textboxes?
Posted
Comments
Kornfeld Eliyahu Peter 6-Jan-14 16:07pm    
Can you show your markup where 'textbox' is declared?

You should handle the Event focus.

Demo - [Demo] Detect TextBox Focus[^]
JavaScript
$("#textbox").focus(function () {
    alert("TextBox is Focused. The Div will Fade Out Now");
    $('#div').fadeOut(2000);
});


Note the Changes here...

#textbox -> # is appended to select the TextBox with ID.
$('#div').fadeOut(2000); -> quotes around #div were missing.
 
Share this answer
 
Comments
joshrduncan2012 7-Jan-14 9:43am    
That worked beautifully. Thank you Tadit! :) My +5.
Most welcome buddy. :)

Thanks for up voting and accepting the answer. :)
Testing both in firefox and chrome, it seems that onfocus triggered first and then click get triggered.

You better work with onmousedown or whatever it is available in jquery. onmousedown would be processed first. then you can decide whether it is focus or click.

To solve the problem that you are actually facing follow Tadit Dash suggestion
 
Share this answer
 
Comments
joshrduncan2012 7-Jan-14 9:43am    
Tadit's answer was successful. Thanks Mohibur.

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