L
L
LordPrimes2018-03-04 17:47:14
JavaScript
LordPrimes, 2018-03-04 17:47:14

What is the best way to write code (ajax)?

Good afternoon, I wrote the code and everything works for me, but it looks bad, can you advise me how to make it better?

$('#input-search').keyup(function(tovar){
  let search = $(this).val();
  if(search != '')
  {
   find(search);
  }
  else
  {
   find();
  }
 function find(tovar)
 {
  $.ajax({
   url:"temp/search.php",
   method:"POST",
   data:{tovar:tovar},
   success:function(data)
   {
    $('.blocktovar').html(data);
    $(".viewtovar ").click(function(){
   let view =  $(this).attr('id');
   $.ajax ({
      url:"temp/view.php",
      method:"POST",
      data:{view:view},
      success:function (data){
          $(".blocktovar").html(data);
          
      }
   });
   });
   }
  });
 }
 });

In short, I made a search engine, and it worked fine, but the full description of the goods stopped working, so I decided to add the code that displays the product on a separate page at the end of the function that is triggered when the ajax code of the search engine is executed

$(".viewtovar ").click(function(){
   let view =  $(this).attr('id');
   $.ajax ({
      url:"temp/view.php",
      method:"POST",
      data:{view:view},
      success:function (data){
          $(".blocktovar").html(data);
          
      }
   });
   });

The output code itself is on a separate page.
I var_dump and ctrl shitv i tracked down what was the problem and this is exactly my js file

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question