2

I'm creating a webpage(e-commerce) and I'm trying to put a search bar with autocomplete suggestions and this is my original code with jQuery:

<script>  
 $(document).ready(function(){  
      $('#products').keyup(function(){  
           var query = $(this).val();  var query = document.querySelectorAll(this).val();
           if(query != '')  
           {  
                $.ajax({  
                     url:"search.php",  
                     method:"POST",  
                     data:{query:query},  
                     success:function(data)  
                     {  
                          $('#productlist').fadeIn();  //
                          $('#productlist').html(data);  
                     }  
                });  
           }  
      });  
      $(document).on('click', 'li', function(){  
           $('#products').val($(this).text());  
           $('#productlist').fadeOut();  
      });  
 });  
 $('#products').keyup(function() {
  if($('#products').val() === '') {
     $('#productlist').hide();
  }
});
</script>

I'm trying not to use any framework so I wanted to turn the code stated above to vanilla javascript and this is the code I currently have right now:

<script>
     
      document.querySelector(document).ready(function(){  
      document.querySelectorAll(".products").keyup(function()
      {  
           var query = document.querySelectorAll(this).value();
           if(query != '')  
           {  
                fetch("search.php")
                .then((response))=>response.json())
                .then(data)=> console.log(data));
            
           });  
           
      }  
      
     });  
     
           element.addEventListener("click", function (){ console.log("clicked");}); 
           document.querySelector(document).addEventListener('click', 'li', function(){  
           document.querySelector('#products').val(document.querySelector(this).text());  
           document.querySelector('#productlist').fadeOut();  
      });  


</script>
zzzz
  • 21
  • 2
  • Hi @zzzz, what exactly is the issue you're having. Please try to be specific. Ideally, use the stack snippets tool to provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – James Feb 07 '21 at 18:00
  • 1
    This may be of interest: https://stackoverflow.com/questions/978799/is-there-an-easy-way-to-convert-jquery-code-to-javascript – Alexander Nied Feb 07 '21 at 18:29

0 Answers0