Enter call Button in Google Chrome and Firefox

Asked

Viewed 144 times

0

I need that when the user presses the enter key, do the button click event. I’m doing it this way, but it’s not working in Google Chrome or firefox:

     if (!IsPostBack)
            {
                txtsenha.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+btnEntrar.UniqueID+"').click();return false;}} else {return true}; ");
            }

I also tried to put this way below, but also does not work in Google Chrome and firefox.

  <asp:Panel ID="Panel2" runat="server" DefaultButton="btnEntrar">
                        <div class="col-xs-6">
                            <div class="input-group">
                                <asp:TextBox ID="Empresa" class="form-control" runat="server" placeholder="Código da Empresa"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-industry" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />
                        <div class="col-xs-7">
                            <div class="input-group">
                                <asp:TextBox ID="txtIdent" class="form-control" runat="server" placeholder="Identificador"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-user fa-lg" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />
                        <br />
                        <div class="col-xs-8">
                            <div class="input-group">
                                <asp:TextBox ID="txtsenha" class="form-control" runat="server" type="password" placeholder="Senha" AutoPostBack="False" OnTextChanged="txtsenha_TextChanged"></asp:TextBox>
                                <span class="input-group-addon">
                                    <button class="fa fa-unlock-alt fa-lg" style="background: transparent; border: none"></button>
                                </span>
                            </div>
                        </div>
                        <br />

                        <asp:Button ID="btnEntrar" runat="server" Text="Entrar " CssClass="radius" OnClick="btnEntrar_Click" />
                    </asp:Panel>

I made this adaptation in the code, but also nothing happens.

    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('#txtsenha').on('keypress', function (e) {
                if (e.keyCode == '13') {
                    jQuery('#btnEntrar').trigger('click');
                }
            })

            jQuery('#btnEntrar').click(function () {
                alert('Click!');
            });

        });
    </script>

<div class="col-xs-8">
                        <div class="input-group">
                            <asp:TextBox ID="txtsenha" class="form-control" runat="server" type="password" placeholder="Senha" AutoPostBack="False" OnTextChanged="txtsenha_TextChanged"></asp:TextBox>
                            <span class="input-group-addon">
                                <button class="fa fa-unlock-alt fa-lg" style="background: transparent; border: none"></button>
                            </span>
                        </div>
                    </div>
                    <br />
                    <asp:Button ID="btnEntrar" runat="server" Text="Entrar " CssClass="radius" OnClick="btnEntrar_Click" />

  • Have you tried using the Defaultbutton="myButton" property? Read more here: https://stackoverflow.com/questions/7638119/how-to-set-a-default-enter-on-a-certain-button

  • I’m trying that way too, but nothing happens.

  • Post your aspx code to see if I can help you.

  • I changed it including the Aspx code, thank you.

  • I really couldn’t see why it didn’t work. Read this link for help: https://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click#_comments

  • Jhonathan I checked the problem is in Google Chrome browsers and Firefox. In IE works normal. But I needed it to work in all these browsers. I’ll see this link q passed me.

Show 1 more comment

2 answers

1

Below is a functional example using jQuery.

jQuery(document).ready(function(){
  jQuery('#senha').on('keyup',function(e){
    if(e.keyCode == '13'){
      jQuery('#entrar').trigger('click');
    }
  })
  
  jQuery('#entrar').click(function(){
    alert('Click!');
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="senha" id="senha" value="" type="password">
<input type="button" value="entrar" name="entrar" id="entrar">

  • Does this example work in all browsers? I adapted the function for my project, but it doesn’t work.

  • You can post your adapted code ?

  • I edited the question with the adapted code.

0


I managed to solve, with this code. Thank you to everyone who helped.

 if (!IsPostBack)
            {
                txtsenha.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnEntrar.ClientID + "').click();return false;}} else {return true}; ");
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.