David Barker
David Barker

Reputation: 14620

Text Input box only accepts Backspace

I'm building a Jquery/JS filtered data table. I have a search input field that on keypress fires an ajax request and then deals with the subsequent response.

All was working great, until I decided I wanted to include the backspace button press as an additional event, checking whether the field value was less than 1.

It worked fine until I added the second .on method that should fire exclusively on a backspace and nothing else.

If anyone asks, the ajaxify_laravel() function works fine and is not a problem in this script.

The result: A text box that will accept nothing other than backspace keypresses. How do I get this working to accept all key presses as normal but to handle backspace seperately? I thought I had it nailed but clearly I don't.

My code:

$('input[name="search"]').on('keypress', function() 
{

    if ($(this).val().length > 1)
    {               
        var request = {
            'filter' : $(this).attr('name'),
            'value'  : $(this).val()
        }, url = "/admin/user/view";

        ajaxify_laravel(url, request, build_user_html, failure);
    }

    return false;

}).on('keydown', function(e) {

    if (e.keyCode == 8 && $(this).val().length < 1)
    {
        var request = {
            'filter' : 'reset'
        }, url = "/admin/user/view";

        ajaxify_laravel(url, request, build_user_html, failure);

    }   
});

Upvotes: 1

Views: 853

Answers (1)

Pointy
Pointy

Reputation: 413712

It looks as if the "keypress" handler is (at least sometimes) returning false. That'll be interpreted as a command to stop processing the event — including the default "native" behavior. That is, it'll cause keypresses to be ignored.

Upvotes: 1

Related Questions