?
Solved

Javascript onfocus not working

Posted on 2007-12-04
9
Medium Priority
?
2,324 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:Munawar Hussain
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:Munawar Hussain
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:Munawar Hussain
ID: 20402377
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!

 
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:Munawar Hussain
ID: 20403150
you may use this to make it readonly
TextBox1.Attributes.Add("readonly","readonly");

thanks
0
 
LVL 12

Expert Comment

by:Munawar Hussain
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:
Munawar Hussain earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

777 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