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();")
txtEmployeeID.Attributes.Add("onkeypress", "this.blur();")
txtEmployeeID.Attributes.Add("onblur", "alert('Blur event fired');")
it works
Does anyone know the reason why this strange behaviour with the onfocus event?
Who is Participating?
Munawar HussainConnect With a Mentor Principal 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 ..
Munawar HussainPrincipal Software EngineerCommented:
this might help you in finding out the issue.
here onfocu and onBlur are discussed in detail.

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)");  

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <script language ="javascript">
    function TestFocus(myControl)
    function TestBlur(myControl)
    //on leave control
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox2" runat="server">bb</asp:TextBox>
        <asp:TextBox ID="TextBox1" runat="server">aa</asp:TextBox></div>

// 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.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Munawar HussainPrincipal Software EngineerCommented:
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.
Munawar HussainPrincipal Software EngineerCommented:
you may use this to make it readonly

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.
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
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.