Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know if input is in use and disable the hotkey (Jquery)

I use some hotkeys on my website, but when the user is inside the search form or inside comment. I want to disable them.

What the best for me to do it? Thanks

Example of my hotkey:

$(document).keydown(function(e)
{
    if (e.which == 40 || e.which == 74) // next post
    { 
        return scroll('next');
    }

    if (e.which == 38 || e.which == 75) // prev post
    { 
        return scroll('prev');
    }
});
like image 404
Tahola Avatar asked Dec 16 '25 16:12

Tahola


2 Answers

You can check for the event.target element. If that element is from type INPUT you might want to omit the handler code. Could look like

$(document).keydown(function(e)
    {
        if( e.target.nodeName !== 'INPUT' ) {
            if (e.which == 40 || e.which == 74) // next post
            { 
                return scroll('next');
            }

            if (e.which == 38 || e.which == 75) // prev post
            { 
                return scroll('prev');
            }
        }
});
like image 82
jAndy Avatar answered Dec 19 '25 05:12

jAndy


You could check if e.target.nodeName === INPUT (the event is triggered inside an input field) and act accordingly

like image 38
Nicola Peluchetti Avatar answered Dec 19 '25 06:12

Nicola Peluchetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!