I would like to take the case sensitive out of a search in . PHP

Asked

Viewed 144 times

-4

<div class="divBusca">
                  <input class="txtBusca" type="text" name="Tópicos"
                  id="txtBusca" placeholder="Buscar por item..."/>
                  <br><br><br>
          </div>
          <br>
        <ul id="ulItens">
         <li class="pushy-link"><a href=""> Agenda </a></li>

                  <li class="pushy-submenu">
                    <button> Pedidos </button>
                    <ul>
                        <li class="pushy-link"><a href=""> + Pedidos </a></li>
                        <li class="pushy-link"><a href=""> Todos Pedidos </a></li> 
                        <li class="pushy-link"><a href=""> Todos Pedidos LOJAVIRTUAL </a></li> 
                        <li class="pushy-link"><a href=""> Acompanhamento das vendas online </a></li> 
                        <li class="pushy-link"><a href=""> Boletos Vencidos </a></li>
                    </ul>
                  </li>

                  <li class="pushy-submenu">
                    <button> eCommerce </button>
                    <ul>
                        <li class="pushy-link"><a href=""> Todos Pedidos </a></li>
                        <li class="pushy-link"><a href=""> Total itens </a></li>
                        <li class="pushy-link"><a href=""> Importar</a></li> 
                        <li class="pushy-link"><a href=""> Importar manual </a></li> 
                        <li class="pushy-link"><a href=""> Faturamento </a></li> 
                    </ul>
                  </li>
        </ul>
<script type="text/javascript">
$(function(){
    $("#txtBusca").keyup(function(){
        var texto = $(this).val();
        $("#ulItens li").css("display", "block");

        $("#ulItens li").each(function(){
            if($(this).text().indexOf(texto) < 0){
                $(this).css("display", "none");
            }

        });
    });
});
</script>
  • 2

    What is this search? Where PHP is in the story?

3 answers

0

Put everything in uppercase. For example:

var texto = $(this).val().toUpperCase();
$("#ulItens li").css("display", "block");

$("#ulItens li").each(function(){
    if($(this).text().toUpperCase().indexOf(texto) < 0){
        $(this).css("display", "none");
    }
});

0


welcome to stackoverflow.

Unfortunately javascript does not have a specific function to disable case sensitive, but you can do your search by treating your input and the string you want to compare.

Try converting the search entry all to lower or upper case and the string you want to compare as well (before performing the comparison).

In your code try changing:

  • var texto = $(this).val(); for var texto = $(this).val().toLowerCase();
  • if($(this).text().indexOf(texto) for if($(this).text().toLowerCase().indexOf(texto)

Your modified code should look like this:

<script type="text/javascript">
$(function(){
    $("#txtBusca").keyup(function(){
        var texto = $(this).val().toLowerCase();
        $("#ulItens li").css("display", "block");

        $("#ulItens li").each(function(){
            if($(this).text().toLowerCase().indexOf(texto) < 0){
                $(this).css("display", "none");
            }

        });
    });
});
</script>

Observing

  • The function toLowerCase() in javascript converts the letters of a string to minúcuslas
  • The function toUpperCase() converts the letters of a string to CAPITAL LETTERS.
  • It worked thanks! How would I read independent of the accent tbm?

0

This is not PHP is Javascript.

You can convert both search and search pattern to uppercase or lowercase.

Insensitive search by converting texts to lowercase:

if( $(this).text().toLowerCase().indexOf(texto.toLowerCase()) < 0){
        $(this).css("display", "none");
  }

Insensitive search converting both texts to uppercase :

if( $(this).text().toUpperCase().indexOf(texto.toUpperCase()) < 0){
        $(this).css("display", "none");
  }
  • How would I read independent of the stress tbm?

Browser other questions tagged

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