0

My HTML is a list as bellow:

<ul class="friend_list">
    <li> 
        <a href="#">Krishna</a>
        <a href="#">Karan</a>
        <a href="#">Ram</a>
        <a href="#">Hari</a>
    </li>
</ul>

<input type="text" name="sort" value="Search" id="sort">

Here when a user enters any string in the input text #sort it should sort the value list according to match. Like if I enter K in the input Krishna and Karan list should show and Hari should hide. And again When I enter H or Hari, the Hari li should display and others should hide.

#sort key up function should filter result as the SQL LIKE Operator does.

Can anyone help me find a solution?

4
  • 1
    possible duplicate of jQuery table sort Commented Jul 18, 2013 at 12:31
  • do you want it only with the first letter of the name? or should it be like letter% or %letter%. And is it a requirement that it is only one letter? Commented Jul 18, 2013 at 12:31
  • This isn't so much of a sort as you're hiding non relevant items. It's a filter. Commented Jul 18, 2013 at 12:32
  • i want %letter% @Spokey Commented Jul 18, 2013 at 12:44

2 Answers 2

3

Here is an idea:

 $("#sort").keyup(function(){
      var v = $(this).val();
      if(v.length==0)
      {    $("a").show();}
      else
      {
      $("a").each(function(){
          if($(this).text().indexOf(v)==0)
              $(this).show();
          else
              $(this).hide();
      })
      }
  }) ;

http://jsfiddle.net/fqyCM/

Change

if($(this).text().indexOf(v)==0) 

to

if($(this).text().indexOf(v)!=-1) 

to change from starts with to contains

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

Comments

0

you do not provide any code, I doubt anyone is going to do it for you. Here is a code that might give you what you want : http://codecanyon.net/item/advanced-tables/full_screen_preview/53366?ref=lvraa I'd recommand you use a jQuery plugin for this if you not very experimented with programming.

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.