Question

I want to run the code bellow only when user is outside textarea:

$(document).on('keydown', function(e){
    if (e.which == 37) {
        mySwiper.swipePrev();
    }
    if (e.which == 39) {
        mySwiper.swipeNext();
    }
});

This is what I use to certify user is leaving textarea:

$('textarea').bind('blur', function() {
    $(this).css({
        'background-color':'#E18B6B'
    });
});

Any help will be a joy. Thank you.

Was it helpful?

Solution

You can check before firing the event if the text area is in focus.

$("textarea").is(":focus");

To extend your code:

$(document).on('keydown', function(e){

    if (e.which == 37 && !$("textarea").is(":focus")) {
        mySwiper.swipePrev();
    }
    if (e.which == 39 && !$("textarea").is(":focus")) {
        mySwiper.swipeNext();
    }

});

OTHER TIPS

Just confirm that the event.target isn't a textarea.

$(document).on('keydown', function(e){
    if ( $(e.target).is("textarea") ) {
        return; // exit early
    }
    if (e.which == 37) {
        mySwiper.swipePrev();
    }
    if (e.which == 39) {
        mySwiper.swipeNext();
    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top