Simulate enter jquery

1

I need to simulate an enter in jquery I have the following code:

$('').trigger(jQuery.Event('keypress', { keycode: 13 }));

But this is done on an element, with an id or class, I need to generate the enter automatically without needing to refer to an element. I hope to explain myself, I am attentive to your answers.

    
asked by Javier Antonio Aguayo Aguilar 08.06.2017 в 00:50
source

2 answers

3

Use $ ('body') as a selector, I'll give you an example

//Cuando se presione enter
$('body').keypress(function (e) {
    if(e.which == 13){
        alert('ok');
    }
});

//Simula que se presiona la tecla enter 
$('body').trigger({
    type: 'keypress',
    which: 13
});
    
answered by 08.06.2017 в 00:59
0

Try to see if this can help you ... luck.

$(function(){
    $(document).on('click', function() {
        $(document).trigger($.Event('keypress', { keycode: 13 }));
    });
});
    
answered by 08.06.2017 в 17:34