2

Basically:

  1. I have a page with a textbox, and a <ul> list below it. The <ul> is populated by a list of the user's friends.

  2. The user begins typing in the name of a friend in the textbox, e.g pressing 'r'

  3. I want to immediately update the <ul> with each keypress to show only those friends whose names begin with R, e.g 'Richard, Redmond, Raheem', etc.

  4. As the user types more, I want to further restrict the names, e.g if user types 'Ri' then I only want 'Richard' in the list.

I'm looking for ideas on how to implement the searching. Specifically, if I should use an Array or JSON class for storing the list of friends, if there's any regular expression I should use, etc?

Also which jQuery event should I use for listening to the keypress events?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ali
  • 261,656
  • 265
  • 575
  • 769

2 Answers2

7

Working example: http://jsfiddle.net/peeter/gAAth/

This is how I'd do it, I wouldn't duplicate data in an array.

An optimized version provide by Raynos: http://jsfiddle.net/gAAth/12/

Peeter
  • 9,282
  • 5
  • 36
  • 53
  • I can imagine that this is not a good approach if you have a fairly long list (calling `map` on every `keypress`?). But that needs to be tested first... – Felix Kling Apr 16 '11 at 15:10
  • Can you change this to show how this would be implemented if the users' friends' names had to be hard coded in the javascript? – Ali Apr 16 '11 at 15:38
  • Right now its too closely coupled with the UI, if I wanted to shift the names from a `
      ` to a table, or put the names inside a hyperlink, it would break this code. Putting the names externally in javascript seems better. Also the javascript is responsible for populating the `
        ` in the first place.
    – Ali Apr 16 '11 at 16:53
  • Any hint why this is not working in IE? I guess there might be a simple fix. – mtk Jun 05 '13 at 13:05
  • The search doesn't work when you search with a capital letter: Working example the possibility to have capitals in your search field and with multiple lists to search from: http://jsfiddle.net/Proz0r/Ks48H/ – Joeri Minnekeer Nov 20 '13 at 11:08
1

example

Another option based on the @Peeter code .

HTML:

<input type="text" id="input1"/>
<ul id="list">
    <li>Rich</li>
    <li>Rajim</li>
    <li>Andres</li>
    <li>Jorge</li>
    <li>Pedro</li>
    ...
    <li>Raul</li>
    <li>Paula</li>
    <li>Delfina</li>
</ul>

CSS:

li.h {display:none;}

JS:

containsi selector

$.extend($.expr[':'], {
  'containsi': function(elem, i, match, array)
  {
    return (elem.textContent || elem.innerText || '').toLowerCase()
    .indexOf((match[3] || "").toLowerCase()) >= 0;
  }
});

// first option    
$("#input1").keyup(function() {
    var search = $(this).val().toLowerCase();
     $._list = ($._list) ? $._list : $("#list li"); //cache
     $._list
        .removeClass("h")
        .filter(":not(:containsi('"+search+"'))").addClass("h");
});

EDIT

I think a little in the code written, I like the option to hide first and then display.

example

JS:

// second option
$("#input1").keyup(function() {
    var search = $(this).val().toLowerCase();
    $._list = ($._list) ? $._list : $("#list li");
    $._list
        .addClass(function(index, currentClass) {
            var addedClass;
            if (currentClass !== "h" )
                addedClass = "h";
            return addedClass;
        }).filter(":containsi('"+search+"')").removeClass("h");
});

// third opcion
$("#input1").keyup(function() {
    var search = $(this).val().toLowerCase();
    $._list = ($._list) ? $._list : $("#list li");
    $._list
        .hide()
        .filter(":containsi('"+search+"')").show();
});
Community
  • 1
  • 1
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
  • Can you please give an overview of the difference between your answer and the other one? People shouldn't have to read both in detail just to get basic info like that. – Ian Dunn May 18 '15 at 19:07