views:

1567

answers:

2

I have a repeater that outputs divs like the following for every item returned from some method.

<div class="editor-area">
    <div class="title">the title</div>
    <div>the description</div>
    <div class="bottom-bar">
        <a href="link">Modify</a>
        <a href="link2">Delete</a>
    </div>
</div>

I need to have a textbox on the page that allows the user to filter the list based on what's in the title field. I would like it to happen as the user types.

I could get this done without asking for help, but I want to do it right. I'm using ASP.Net 2.0 WebForms (unfortunately), and I can use jQuery if it would be useful for this (i have very little experience with it).

Any tips or samples would be appreciated.

If the filter operation takes a couple of seconds, how do you keep it from locking up the screen? What event should I do the filter on? Is there anything in jQuery that would make the javascript a little cleaner?

+6  A: 

Yes, this is dead simple with jQuery. First hide everything:

$("div.title").hide();

(Matches elements of type "div" with class "title".) Now show the matches:

$("div.title:contains(searchText)").show();

Help for "contains".

It should not take "seconds" to do this unless your page is enormous. You can do this in onKeyDown and onChange.

Craig Stuntz
I need to hide the parent div of title (which I've not given another class and added that to the selector on the first). But I don't know how to make it show the parent div of the search results.
Max Schmeling
Also, when I hit the shift key all items disappear so I need to account for non-text input. Should I just put that in an if that checks for text in the filter textbox?
Max Schmeling
$("div.title").parent().hide();$("div.title:contains(searchText)").parent().show();Yes, check for text before hiding/showing.
Craig Stuntz
You are the man! So fast! Thanks.
Max
+2  A: 

Craig is very close. Put ".parent()" before ".hide()" or ".show()" to show or hide the parent div.

As for your second comment, that's a separate problem but yes, you'll need to account for non-text input. Another idea is to show everything if the filter textbox is blank.

Dan Goldstein
I put this in the onkeyup of the textbox:javascript:$('div.title').parent().hide(); $('div.title:contains(this.value)').parent().show();It hides all of the divs, but never shows them again.
Max Schmeling