• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2328
  • Last Modified:

Javascript onfocus not working

The onfocus event is not firing for a textbox when the code is written as below:
txtEmployeeID.Attributes.Add("onfocus", "this.blur();")
When the same code is written for other events as:
txtEmployeeID.Attributes.Add("onclick", "this.blur();")
OR
txtEmployeeID.Attributes.Add("onkeypress", "this.blur();")
OR
txtEmployeeID.Attributes.Add("onblur", "alert('Blur event fired');")
it works
Does anyone know the reason why this strange behaviour with the onfocus event?
0
psid23
Asked:
psid23
  • 6
  • 3
1 Solution
 
Munawar HussainPrincipal Software EngineerCommented:
this might help you in finding out the issue.
here onfocu and onBlur are discussed in detail.
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21774160.html

Thanks
0
 
Munawar HussainPrincipal Software EngineerCommented:
both are working fine
here is working code for that..

//class file
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onfocus", "TestFocus(this)");
        TextBox1.Attributes.Add("onblur", "TestBlur(this)");  
    }

//ASPX FILE
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language ="javascript">
    function TestFocus(myControl)
    {
    alert('focus');
    }
    function TestBlur(myControl)
    {
    //on leave control
    alert('blur');
    //alert(myControl.value);  
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;
        <asp:TextBox ID="TextBox2" runat="server">bb</asp:TextBox>
        <asp:TextBox ID="TextBox1" runat="server">aa</asp:TextBox></div>
    </form>
</body>
</html>


// when a control get focus then onfocus event is called and when a control leaves focus then the blur event is called.. according to that the above example is working perfectly.
thanks
0
 
Munawar HussainPrincipal Software EngineerCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
psid23Author Commented:
Thanks needo_jee but I am aware of all the solutions you have.

The problem is that all events are firing except the onfocus event for my code.

I am not able to find out why only the onfocus event is not working.

Can we have an alternate solution to make the textbox read-only using javascript?

There is a certain restriction by virtue of which I cannot directly make the textbox readonly and I need to do this via javascript only.
0
 
Munawar HussainPrincipal Software EngineerCommented:
you may use this to make it readonly
TextBox1.Attributes.Add("readonly","readonly");

thanks
0
 
Munawar HussainPrincipal Software EngineerCommented:
I can explain pros and cons of that if required .. but for your requiremets to make a textbox readonly above comments would work fine.
thanks
0
 
psid23Author Commented:
thanks needo_jee
bit I want to know how the textbox can be made readonly without setting its readonly property but using javascript
0
 
Munawar HussainPrincipal Software EngineerCommented:
well with JS this line or the one you wrote in your question seems working perfectly to make a textbox readonly

 TextBox1.Attributes.Add("onfocus", "this.blur();")

also, you are using IE 6 there should be no problem .. thought there is with some of the browsers version ..
http://www.webreference.com/programming/javascript/beginning/chap6/3/
0
 
psid23Author Commented:
Thanks needo_jee
Your solutions were right to the point
Howevere, the problem was with a javascript menu used in the site that was causing the problem
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now