Solved

Javascript onfocus not working

Posted on 2007-12-04
9
2,321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

761 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