The Muffin Man
The Muffin Man

Reputation: 20004

Help with jQuery show/hide on keyup

Basically I have a text box and when I type characters into the box it hides rows in my table that don't contain the text. How can I reform the following code so that it re-evaluates the function so that it starts showing the rows again when I press backspace?

This is as far as I got:

$("#txtFilter").keyup(function () {
                var text = $("#txtFilter").val();
                if () {
                    $(".tableStripe .tdName:not(:contains(" + text + "))").closest("tr").show();
                }
                else { 
                    $(".tableStripe .tdName:not(:contains(" + text + "))").closest("tr").hide();
                }
            });

Upvotes: 0

Views: 1468

Answers (3)

jerjer
jerjer

Reputation: 8760

Try this one:

$("#txtFilter").keyup(function () {
      $(".tableStripe tr").hide();
      setTimeout(function(){
            var text = $("#txtFilter").val();                                
            $(".tableStripe .tdName:not(:contains(" + text + "))").closest("tr").show();
      },10);
});

Upvotes: 0

Rahul Chordiya
Rahul Chordiya

Reputation: 542

try this one

var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 8) { //Enter keycode
   //Do something
 }

Upvotes: 1

Calvin
Calvin

Reputation: 8765

You need to add the event var to your callback function, then specify the keycode for backspace:

$("#txtFilter").keyup(function(event) {
    var text = $("#txtFilter").val();
    if (event.keyCode == 8) { // 8 = keycode for backspace
        $(".tableStripe .tdName:not(:contains(" + text + "))").closest("tr").show();
    } else { 
        $(".tableStripe .tdName:not(:contains(" + text + "))").closest("tr").hide();
    }
});

For more javascript keycodes, do a google search for 'javascript keycodes'

Upvotes: 1

Related Questions