"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, February 3, 2013

Moving focus around the table in Jquery

Last week i answered a question in a fourm ( http://forums.asp.net/p/1877841/5281857.aspx/1?How+can+I+move+the+focus+around+my+html+table+ ) to move focus around the cells in a table using jquery . i go with keypress event. below it goes.


<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title></title>
    <script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(':text').keypress(function (e) {
                var keyCode = e.keyCode || e.which;
                var SideMax = 3;
                var SideMin = 1;
                var Upmax = 1;
                var Upmin = 3;
                var Row = +$(this).attr("attr-Row");
                var Col = +$(this).attr("attr-Colum");
                arrow = { left: 37, up: 38, right: 39, down: 40 };
                if (e.ctrlKey) {
                    switch (keyCode) {
                        case 37:
                            if (Col <= SideMax) {
                                SetFocuz(Row + '' + Col - 1);
                            }
                            break;
                        case 39:
                            if (Col >= SideMin) {
                                Col = Col + 1;
                                SetFocuz(Row + '' + Col);
                            }
                            break;
                        case 38:
                            if (Col < Upmax) {
                                SetFocuz(Row - 1 + '' + Col);
                            }
                            break;
                        case 40:
                            if (Col < Upmin) {
                                SetFocuz(Row + 1 + '' + Col);
                            }
                            break;
                    }
                }
            });
        });
        function SetFocuz(attrValue) {
            alert(attrValue);
            $(':Text').each(function () {
                if ($(this).attr('attrFocuz') == attrValue) {
                    $(this).focus();
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="txt01" attr-Row="1" attr-Colum="1" attrFocuz="11" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" attr-Row="1" attr-Colum="2" attrFocuz="12" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" attr-Row="1" attr-Colum="3" attrFocuz="13" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox3" attr-Row="2" attr-Colum="1" attrFocuz="21" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox4" attr-Row="2" attr-Colum="2" attrFocuz="22" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox5" attr-Row="2" attr-Colum="3" attrFocuz="23" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox6" attr-Row="3" attr-Colum="1" attrFocuz="31" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox7" attr-Row="3" attr-Colum="2" attrFocuz="32" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox8" attr-Row="3" attr-Colum="3" attrFocuz="33" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>




If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: