Solved

Javascript onfocus not working

Posted on 2007-12-04
9
2,317 Views
Last Modified: 2010-04-21
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
Comment
Question by:psid23
  • 6
  • 3
9 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 20402296
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
 
LVL 12

Expert Comment

by:needo_jee
ID: 20402370
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
 
LVL 12

Expert Comment

by:needo_jee
ID: 20402377
0
 
LVL 1

Author Comment

by:psid23
ID: 20402836
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:needo_jee
ID: 20403150
you may use this to make it readonly
TextBox1.Attributes.Add("readonly","readonly");

thanks
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 20403171
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
 
LVL 1

Author Comment

by:psid23
ID: 20410251
thanks needo_jee
bit I want to know how the textbox can be made readonly without setting its readonly property but using javascript
0
 
LVL 12

Accepted Solution

by:
needo_jee earned 500 total points
ID: 20410665
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
 
LVL 1

Author Closing Comment

by:psid23
ID: 31412559
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now