Follow link with Enter based user input

Asked

Viewed 143 times

0

With the help of the community, I created a file to redirect a user who has typed their login to a web page that receives the user in this way:

https://site.com.br/usuario=LOGIN&empresa

The problem is that usually the user presses Enter when typing the text, so I tried to adjust the code so that when pressing this key was redirected, but apparently the link is not receiving the variable.

Could help me, by pressing Enter or clicking Sign in after entering your login, the user is directed?

Just follow my code:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<title>Redirecionamento</title>
<div class="page-margin"></div>
<br>
<br>
<div align="center" class="welcome-form-div">
  <div class="welcome-form">
    <div class="html-logo">
      <i class="icon-comments-alt"></i> Insira os números do seu
      <br><b>login</b> <i>ou</i> <b>CPF</b> e clique em Entrar
      <br>
      <br>
    </div>

    <input type="text" id="username" name="username" size="20">
    <input type="button" id="BotaoLogin" value="Entrar" onClick="redirecionar()">
</div>


<script language="javascript">

var username = document.getElementById("username");

username.addEventListener("keydown", function(e) {
  if (e.keyCode == 13) {
    redirecionar(e);
  }
});

function redirecionar() {
  if (username != "") {
    window.location = "https://site.com.br/usuario"+username+"&empresa";
  }
}
</script>

1 answer

0


function redirecionar() {
  var username = document.getElementById("username").value;
  if (username != "") {
    window.location = "https://site.com.br/usuario"+username+"&empresa";
  }
}

Missing capture element value.

  • Perfect! It worked! Thank you.

Browser other questions tagged

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