Javascript Form How to - Call a javascript function from a text field without submit button








Question

We would like to know how to call a javascript function from a text field without submit button.

Answer


<!DOCTYPE html>
<html>
<body>
  <input type="text" id="url" />
    <script type='text/javascript'>
    <!--  w  w  w  .  j a v a2  s.c  om-->
    var input = document.getElementById('url');
    input.onkeydown = function(e) {
      var key = e.keyCode || e.which;
        if (key == 13) {
           console.log("enter");
           window.location = input.value;
        }
    };
    </script>

</body>
</html>

The code above is rendered as follows: