Home » Javascript » jQuery on(“click”,…) function doesn't execute for my dynamically created div

jQuery on(“click”,…) function doesn't execute for my dynamically created div

Posted by: admin November 1, 2017 Leave a comment

Questions:

I’m creating a to-do list and I want to be able to click on a div with a “to-do” and directly delete it from the database. This is my main page.

    <div class="hoofder">
      <h2>Voegtoe:</h2>
    </div>
         <?php include('connect.php'); ?>
    <form method="post" action="add.php">
      <div class="input">
        <label>Taak</label>
        <input type="text" name="taak">
      </div>
      <div class="input">
        <button type="submit" name="add_todo class="btn">Register</button>
            <?php include('getthings.php'); ?>
        <script src="jquery-3.2.1.min.js"></script>
        <script>
        $(document).ready ( function () {
            alert("1");
         $(document).on("click", ".lijst", function () {
              alert("2.");
                    var del_id = $(this).attr('id').substring(1);
                    alert(del_id);
                    var $ele = $(this).parent();
                    $.ajax({
                        type:'POST',
                        url:'delete.php',
                        data:{del_id:del_id},
                        success: function(data){
                              if(data=="YES"){
                                 $ele.fadeOut().remove();
                             }else{
                                 alert("can't delete the row");
                             }
                        }
                    });
                });
            });
                    alert("end");
     </script>
    </form>
  </body>
</html>

delete.php:

<?php
include 'connect.php';
include 'add.php';
$delete_this = $_POST['del_id'];
$qry = "DELETE FROM tedoen WHERE id ='$delete_this'";
$result = mysqli_query($conn, $qry);
?>

When I load the page the “alert(del_id);” comes back as undefined.
This is a problem as I need it not to be undefined ofcourse. Do you have any idea why?

Answers: