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
Solved

Javascript onfocus not working

Posted on 2007-12-04
9
2,320 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

856 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