Guilherme Longo
Guilherme Longo

Reputation: 655

overriding tab key behavior so it does not goes to next field (only focus out the actual field)

I have a form that searches for addresses on a database. Every input field is expected to receive its values as phone number or street name and when it triggers an event when it loses focus.

This is working but usually users press tab key to perform that search and focus goes to the next field that is firing up its keyup event too. As the next input has no value, it cleans the search that was performed by the first element.

I don´t want to use a button to trigger the event and the way it is (pressing the tab key) is the preferred way to do so how can I prevent that behavior? Could I override the behavior so the tab key does not goes to the next input and only focus out the focused input??

Upvotes: 3

Views: 1453

Answers (1)

vaso123
vaso123

Reputation: 12391

I tested it and it worked. Workin jsFiddle demo.

<input class="x" type="text" value="">

jQuery code

    $('.x').keydown(function(e) {
        if (e.keyCode == 9) {
            e.preventDefault();
        }
    });

Upvotes: 4

Related Questions