Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
JavaScript function only fires when there are 2 input boxes

I have an aspx page in Visual Studio. I have a function which fires on the onkeyup. When I have just the single control on the page, the JavaScript function is not called. When I add a second control, the JavaScript is called.

With this code, the JavaScript does not fire...

<form id="form1" runat="server">
    <div>
        <div class="form-group">
            <label for="barcode">Barcode</label>
            <input type="text" onkeyup="searchBarcode()" class="form-control" id="txtBarcode" name="barcode" />
        </div>
    </div>
</form>
<script>
    function searchBarcode() {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            alert('Hello');
        }
    }
</script>

With this code the JavaScript fires

 <form id="form1" runat="server">
        <div>
            <div class="form-group">
                <label for="registration">Registration</label>
                <input type="text" class="form-control" id="txtRegistration" name="registration" />
            </div>
            <div class="form-group">
                <label for="barcode">Barcode</label>
                <input type="text" onkeyup="searchBarcode()" class="form-control" id="txtBarcode" name="barcode" />
            </div>
        </div>
    </form>
    <script>
        function searchBarcode() {
            var keycode = (event.keyCode ? event.keyCode : event.which);
            if (keycode == '13') {
                alert('Hello');
            }
        }
    </script>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this:

<script>
  window.onload = function () {
            document.getElementById('txtBarcode').addEventListener('keyup', keyEvent)
  }
  function keyEvent(e) {
            var keycode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == '13') {
                alert('Hello');
    }
  }
</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are missing passing event to the function

<form id="form1" runat="server">
    <div>
        <div class="form-group">
            <label for="barcode">Barcode</label>
            <input type="text" onkeyup="searchBarcode(e)" class="form-control" id="txtBarcode" name="barcode" />
        </div>
    </div>
</form>
<script>
    function searchBarcode(event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            alert('Hello');
        }
    }
</script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda