Deteksi enter pada input dengan jquery

Bismillaahirrohmaanirrohiim…

Dengan jquery segala urusan javascript jadi lebih mudah. Contohnya saat kita ingin mendeteksi event saat tombol keyboard ditekan enter pada input.

Berikut untuk kode jquery yang lama:

$('.selector_input').keypress(function(e){
    if(e.which ==13) {
        alert('tombol enter dideteksi');
    }
})

Untuk jquery yang baru, bisa menggunakan kode di bawah ini:

//$(document).on('keydown', 'input.class_selector', function(ev) {
jQuery(document).on('keydown', 'input.class_selector', function(ev) {
    if(ev.key === 'Enter') {
        alert('enter detected');
    }
});

Cukup mudah bukan…

Bagikan
Baca Juga:   Menambahkan mata pada input password

You May Also Like

About the Author: rasupe

Leave a Reply

Discover more from Rasupe

Subscribe now to keep reading and get access to the full archive.

Continue reading