2

How do I select rows depending on what's in an input box? Here's what I have so far:

<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
$(document).ready(function() {
    $('#btnFilter').keyup(function() {
        var myInput = $(this).val();
        $('tbody tr').hide();
        // I need to .show() all table rows that have a table cell with a value that begins with the input field.
        $('tr').something goes here.show();
    });
});
</script>
</head>
<body>
<form>
    <input id="btnFilter" name="btnFilter">
</form>
<table>
    <thead>
        <tr>
            <th>Heading 1</th>
            <th>Heading 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>A</td>
            <td>X</td>
        </tr>
        <tr>
            <td>ABC</td>
            <td>D</td>
        </tr>
    </tbody>
</table>
</body>
</html>
1
  • Not a direct answer, but if you are looking at a way to filter a table, have a look at datatables.net. It's in jQuery, uses your html table structure directly and support pagination, filtering, sorting and much more. It's a great plugin. Commented Oct 4, 2010 at 15:42

2 Answers 2

1

If you want a search, :contains() is probably what you're after, for example:

$('tr:contains("' + myInput + '")').show();

You can try it out here, or a cell-begins-with only version:

$('td').filter(function() {
  return $.text([this]).indexOf(myInput) == 0
}).parent().show();

You can try that version here.

Sign up to request clarification or add additional context in comments.

2 Comments

Zounds! Hey Nick! You're in W-S?! I'm in Hickory! How's the honeymoon?
@cf_PhillipSenn - excellent :) trying to buy a house this week as well ;)
1
$(document).ready(function() {
    $('#btnFilter').keyup(function() {
        var myInput = $(this).val();
        $('tbody tr').hide().find('td:contains("'+myInput+'")').parents('tr:first').show()
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.