My code only filters if you type first letter Jquery, how to filter by typing the whole word (table)?

Asked

Viewed 42 times

-2

Ex: word (ideal) if I type in the search the "i" it searches all the word that contains the letter "i", but if I type all the word the search disappears and is blank.

follows the code:

$('#myInput').keyup(function () {
      var searchitem = $('#myInput').val();
      if (searchitem == null || searchitem == undefined){
          $('#table tbody td').show();

      }
      else {
          searchitem = searchitem.toUpperCase();
          $('#table tbody tr').hide();
          $('#table tbody tr').each(function () {
              if ($(this).text().indexOf(searchitem) >= 0) {
                  $(this).show();
              }
          });     
      }

  });
  • 1

    Please edit the question and elaborate a [mcve]. At [Edit] take the opportunity to also describe how the code behavior should be when the word has only one letter (example: search for the word é).

1 answer

0

Some information is missing to give a more concrete answer but I hope the example below can help you:

$('#myInput').keyup(function () {
	const val = event.target.value.toUpperCase();
	$('#table td').each(function() {
		const tdText = $(this).text().toUpperCase();
		if (tdText.indexOf(val) >= 0) {
			$(this).parent().show();
		} else {
			$(this).parent().hide();
		}
	});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="myInput" />
<table id="table">
	<tbody>
		<tr><td>Lorem</td></tr>
		<tr><td>ipsum</td></tr>
		<tr><td>dolor</td></tr>
		<tr><td>sit</td></tr>
		<tr><td>amet</td></tr>
		<tr><td>consectetur</td></tr>
		<tr><td>adipisicing</td></tr>
		<tr><td>elit</td></tr>
	</tbody>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.