1

please help me, I'm trying to click a hidden button if the user press the ENTER key inside a text box. It works fine with one text box, but if I add another, java script don't work.

TextBox. ------------------------------------------------------------------------------------------

  <pre> 
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>

        <form id="form1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" Text="Button" 
                onclick="Button1_Click1" style="visibility: hidden; display: none;" />

    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <script type="text/javascript">

        var myInput = document.getElementById("TextBox1");
        if (myInput.addEventListener) {
            myInput.addEventListener('keydown', this.keyHandler, false);
        } else if (myInput.attachEvent) {
            myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
        }

        function keyHandler(e) {
            var EnterKEY = 13;
            if (e.keyCode == EnterKEY) {

                if (e.preventDefault) {

                    document.getElementByID("Button1").click();
                    e.preventDefault();
                }
                return false;
            }
        }
    </script>

        &nbsp;
            <br />
            <asp:Label ID="Label1" runat="server" Visible="False"></asp:Label>
        </form>
    </body>
</pre>
    ------------------------------------------------------------------------------

    CODE BEHIND
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click1(object sender, EventArgs e)
            {
                    TextBox2.Text = "It Works";

            }
        }
1
  • thanks for the quick response, im trying to "click" the "Button1" if the user press Enter, in the Button1 code behind I set a new value to TextBox2. Commented Apr 10, 2013 at 22:11

2 Answers 2

2

Try wrapping your javascript code inside of a document.onload function

window.document.onload = function(e){
var myInput = document.getElementById("TextBox1");
        if (myInput.addEventListener) {
            myInput.addEventListener('keydown', this.keyHandler, false);
        } else if (myInput.attachEvent) {
            myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
        }

//adding the second textbox
myInput = document.getElementById("TextBox2");
            if (myInput.addEventListener) {
                myInput.addEventListener('keydown', this.keyHandler, false);
            } else if (myInput.attachEvent) {
                myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
            }

        function keyHandler(e) {
            var EnterKEY = 13;
            if (e.keyCode == EnterKEY) {

                if (e.preventDefault) {

                    document.getElementByID("Button1").click();
                    e.preventDefault();
                }
                return false;
            }
        }
}

so your script is executed once the DOM tree is fully loaded

Sign up to request clarification or add additional context in comments.

2 Comments

hi pollirrata! i wrap the java script code, but it still not working, :(, btw thanks for your help!
I skipped that in the answer, but you need to add the listener to both textboxes.. I'll edit to show that
0

Looking at your code you only bind your keyhandler(e) function to the element TextBox1 but not on TextBox2. Try creating a new binding code also for your TextBox2.

var myInput2 = document.getElementById("TextBox2");
if (myInput2.addEventListener) {
    myInput2.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
    myInput2.attachEvent('onkeydown', this.keyHandler); /* IE hack */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.