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

116
Vistas
Angular js Directive to Fire "click" event on pressing enter key on ANY element

I need to fire the click event using the enter key using a directive. I need it to be a directive because I would like to reuse it.

Ex: A div which has some onclick events bound to it, I will set focus on the div using the tabindex="0" and keyboard enter keydown to fire the click event.

Is this possible? I have tried below code for a directive but it doesnt work.

(function () {
'use strict';

angular
    .module('app.directives')
    .directive('enterKeyInput', enterKeyInput);

enterKeyInput.$inject = [];

/* @ngInject */
function enterKeyInput() {

    var directive = {
        restrict: 'AC',
        link: function (scope, element, attrs) {
            element.bind("keydown keypress", function (event) {
                if (event.keyCode === 13) {
                    element.click();
                }
            });
        }
    };

    return directive;
}

})();

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Found the answer myself :D

(function () { 'use strict';

angular
    .module('app.directives')
    .directive('enterKeyInput', enterKeyInput);

enterKeyInput.$inject = [];

/* @ngInject */
function enterKeyInput() {

    return {
        restrict: 'AC',
        link: function (scope, element, attrs) {
            element.bind('keyup', function (e)  {
                if ([13].indexOf(e.which) !== -1)  {
                    e.preventDefault();
                    e.stopPropagation();
                    element.click();
                }
            });
        }
    };
}

})();

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