Problem with dynamically appended elements

Problem with dynamically appended elements

I am creating something like a shopping-cart in yii2 .
Here is my code:
  1. $('body').on('click', '#materials li', function () {
  2. let hidden = $('[value=' + $(this).attr('data-id') + ']');
  3. console.log(hidden);
  4. if (hidden.length > 0) {
  5. return false;
  6. }
  7. else {
  8. $('#materials-form').append('<input type="hidden" name ="Materials[' + $(this).attr('data-id') + ']" class="hiddenInput" value="' + $(this).attr('data-supplier') + '"/>');
  9. $('#materials-form').append('<li>' + $(this).text() + '<button data-id = "' + $(this).attr('data-id') + '">Delete</button>' + '<input type ="text" id = "quantity" name="quantity[' + $(this).attr('data-id') + ']" /> ' + '<input type ="hidden" id="price" name="price[' + $(this).attr('data-id') + ']" value ="' + $(this).attr('data-price') + '"' + '</li>');

  10. $("#quantity").keyup(function () {
  11. var sVal = this.value;
  12. var iNum = parseInt(sVal);
  13. var price = $("#price").val();
  14. //var price_num = parseInt(price);
  15. //alert(price);
  16. var total = iNum * price;
  17. $('#materials-form').append('<li>' + total + '</li>');
  18. alert((total));
  19. });}

And I try to check, whether a li is clicked in order not to be appended 2 times. So I declared a let value named hidden and I pass in there the value , or better the id value.
The question is that , the length of that let var , does not increase or it remains always the same. It has a different behavior for different database records.I want the length of that value to increase by click in order not to append it again or if it is not clicked to append?


Here is the rest of the code:
  1. public
  2. function actionGetOrderMaterialsById()
  3. {
  4. \Yii::$app->response->format = 'json';
  5. $supplier_clicked = Yii::$app->request->get("supplier");
  6. $result = (new Query())
  7. ->select('materials.id,materials.name as material,material_supplier.materials_id,material_supplier.price,supplier.name,supplier.id as supplier_id')
  8. ->from('materials')
  9. ->innerJoin('material_supplier', 'materials.id = material_supplier.materials_id')
  10. ->innerJoin('supplier', 'supplier.id = material_supplier.supplier_id')
  11. ->where(['supplier_id'=> $supplier_clicked])
  12. ->all();

  13. //die(var_dump($result));
  14. $options = '';
  15. foreach ($result as $material) {

  16. $options .= '<li id ="li-value" data-supplier = "' . $material['supplier_id'] . '" data-id = "' . $material['id'] . '"data-price = "' . $material['price'] . '" >' . $material['material'] . ' from supplier->
  17. ' . $material['name'] . ' and it costs' . $material['price'] . 'dollars</li>';
  18. }
  19. return $options;


  20. }


  1. <?php $form = ActiveForm::begin(['id' => 'materials-form']); ?>

  2. <input type ="hidden" id="supplier_id" value = "<?php echo $_GET["supplier"]; ?>">

  3. <input type="text" id="search"/>
  4. <ul name="Materials" id = "materials">

  5. </ul>

  6. <!--<input type="text" name="OrderMaterial[value][]" id="quantity" style="display: none;">-->
  7. <input type="submit" value="Submit" id = "submit"/>
  8. <div class="total"></div>