0

in below example , searchForward gets called when Shift key is pressed
however searchBackward never gets called when Shift + tab key is pressed. Please suggest.

$(document).ready(function() {

    $('textarea').live('keydown', function(e) {
      // var keyCode = e.keyCode || e.which;
        if (e.which == 9 ) {    
        var currentIndex = getCaret($(e.target).get(0))
        searchForward($(e.target), currentIndex);
        return false
        } 
        if (e.shiftkey) {
         var currentIndex = getCaret($(e.target).get(0))
        searchBackward($(e.target), currentIndex);
        return false 
       }
    });
});
ronan
  • 4,492
  • 13
  • 47
  • 67

1 Answers1

0

On your second if, you are only checking for the shift key, not for shift and the key pressed. You need to put your second if loop inside the first. Also, I beleive you meant shiftKey, not shiftkey. JavaScript is case sensitive. Take a look at the updated snippet (also moved duplicate code outside the if): JSFiddle

$(document).ready(function() {
    $('textarea').live('keydown', function(e) {
        var currentIndex = getCaret($(e.target).get(0))
        if (e.which == 9 ) {
            if (e.shiftKey) {
                searchBackward($(e.target), currentIndex);
            } else {
                searchForward($(e.target), currentIndex);
            }
            return false
        }
    });
});

Also see JQuery or JavaScript: How determine if shift key being pressed while clicking anchor tag hyperlink?

Community
  • 1
  • 1