Skip to content
Advertisement

Redirect User On Enter Key Based on Text Field Value

I’m trying to simulate some software and I need the user to be able to enter some text into a field and then direct them to another page based on that text. I’ve created the code below but it doesn’t seem to work. I’m using IE 7 and it just seems to refresh the page, it won’t actually go to the new location. Anyone see why? Using jQuery 1.4.4 min.

Updated based on comments still not working

<form id="TEST"><input type="text" id="command" tabindex="1"></form>


<script type="text/javascript">
$(document).ready(function() {
$('#command').keypress(function(e) {
    if (e.which === 13) {
        if ($('#command').value === "AI") {
            window.location = "http://www.mozilla.org";
        }
        if ($('#command').value === "BRI") {
            window.location = "http://www.google.com";
        }
        }
    });
    });
</script>

Advertisement

Answer

The following is working:

<form method="post" id="TEST">
<input type="text" id="command" tabindex="1">
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"  type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#command').keydown(function(e) {

    if (e.which === 13) {
      if ($('#command').val() === "AI") {
        window.location = "http://www.mozilla.org";
      }
      if ($('#command').val() === "BRI") {
        window.location = "http://www.google.com";
      }
      return false;
    }
  });
});
</script>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement