Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all..
i have one text box in a web page i need to set courser position every time in that text box when clicked out side of text box also courser should be in text box only..
i tried like
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Set Courser Focus</title>
    <script src="js/jquery-1.6.2.min.js" type="text/javascript"></script>
    <link href="js/jquery-ui-1.8.11.custom.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>
         <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            //
            // Click Event
            $("#body1").click(function(event) {
                $('#txtname').focus();
            });
        });
    </script>   
</head>
<body id="body1" onload="$('#txtname').focus();">
    <form id="form1" runat="server">
    <div >
    <asp:TextBox ID="txtname"  AutoCompleteType="None"  runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

case1: on page load it is working
case2: on clicking every where in body tag it is focusing on text box
case3:but it is not focusing on text box when outside body mouse click
i need to set always courser position in text box
can any one help me please
thanks in advance..
Posted
Updated 8-Feb-13 22:26pm
v2

1 solution

Instead of handling the click event on body1, try onblur event on the textbox itself. Something like -
JavaScript
$(document).ready(function() {
            //
            // Click Event
            $("#txtname").blur(function(event) {
                $('#txtname').focus();
            });
        });
 
Share this answer
 
Comments
[no name] 10-Feb-13 2:12am    
Perfect man. My +5
Nitesh Kejriwal 10-Feb-13 7:40am    
Thanks Sheikh!
tram7 10-Feb-13 8:25am    
you know what it is not working
just check it once
tram7 10-Feb-13 8:44am    
it is getting solved by redefining code as:
$(document).ready(function() {
$('#txtname').focus();
$("#txtname").blur(function() {
location.reload();
});
});
but your code is root for this..
any way thank you so much..
Nitesh Kejriwal 10-Feb-13 11:53am    
tram7, I wrote the code from my mind and have not tested it. Glad that it helped you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900