Avatar of Whing Dela Cruz
Whing Dela Cruz
Flag for Anguilla asked on

Filktering Alphabetically

Hi Experts, I have found codes for filtering item but I observed that it only works when I type first 3 letters of the item. I want the item to show at the time I encode its first letter. For example if I type H, I want the all items who start with H will show alphabetically. I have no idea it is possible and please give me a little overview if it is not. Thanks!

function mySearch()
{
    var input, filter, table, tr, td, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i++)
    {
    td = tr[i].getElementsByTagName("td")[1];
    if (td)
    {
    if (td.innerHTML.toUpperCase().indexOf(filter) > -1)
    {
    tr[i].style.display = "";
    }
    else
    {
    tr[i].style.display = "none";
    }
    }
    }
}
JavaScriptHTMLASP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
SOLUTION
Banshi lal dangi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

You need to show us the Event code that triggers this - the code that determines the number of letters that must be typed is not in the code you posted.
Whing Dela Cruz

ASKER
Hi Experts, I think this is the one you need,

<fieldset class="fieldSt">
      <input type="text" id="myInput" onkeyup="javascript:mySearch()" class="i19" placeholder="Search for an items..">
 </fieldset>
Julian Hansen

I don't see anything that restricts it to 3 chars.
Here is my sample code
<fieldset class="fieldSt">
  <input type="text" id="myInput" onkeyup="mySearch(this, myTable)" class="i19" placeholder="Search for an items..">
 </fieldset> 
<table id="myTable">
  <tr>
    <td>DOG</td>
    <td>CAT</td>
    <td>WHALE</td>
  </tr>
  <tr>
    <td>HOUND</td>
    <td>PIG</td>
    <td>GOAT</td>
  </tr>
  <tr>
    <td>DOG</td>
    <td>CAT</td>
    <td>WHALE</td>
  </tr>
</table>

Open in new window

JavaScript - note I changed your implementation a bit - it is functionally the same just uses a bit more of what JavaScript / DOM offers us to achieve the same result.
<script>
function mySearch(inp, table)
{
  var filter = inp.value.toUpperCase();
  for(var i = 0; i < table.rows.length; i++) {
    var tr = table.rows[i];
    var cell = tr.cells[1];
    if (cell.textContent.toUpperCase().indexOf(filter) > -1) {
      tr.style.display = 'table-row';
    }
    else {
      tr.style.display = 'none';
    }
  }
} 
</script>

Open in new window

Working sample here
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Whing Dela Cruz

ASKER
Hi Julian, I added some items to let you see the problem I have seen on my project. Please see it on the attached file. When I type letter "L" to find the item "Lidocaine 20mg Amp" the item CATERPILLAR will go it first not unless if i type "Li".
TableSearch.html
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Whing Dela Cruz

ASKER
Its running on my project sir, thank you so much!
Julian Hansen

You are welcome.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.