0

I'm having a minor but rather annoying issue with a webpage I'm making. So I have a link that, when clicked, makes an AJAX request to a PHP script (the link sends a hidden input value, if that matters). It works fine and it works as I expect, but then if I try to click the link again, it doesn't do anything. I have to refresh the page in order for it to work again, and I prefer to not have to do this.

Link: <a name='delete-link' href='#'>Delete</a>

JavaScript:

    $("a[name='delete-link']").click(function(e){
        e.preventDefault();
        var ID = $(this).parents("ul").find("input[name='delete-identifier']").val();

        $.ajax({
            type: 'POST', 
            url: '../ajax.php',
            data: {delete: 'true', delete_id: ID},
            success: function(response){
                $("#result").html(response).fadeIn(500).delay(5000).fadeOut(500);
                $("div[name='update']").load("test.php #update");
            }
        })
    })

The only code in the #update div is this PHP code which echoes a table displaying users:

<?php
    if ($rs){
        $user_table = "<table class='table table-bordered table-condensed table-hover table-striped'><th>Action</th>";
        $user_table .= "<tr><th>Name</th><th>Privilege ID</th></tr>";
        while ($user = $rs->fetch_array(MYSQL_ASSOC)){
            $user_table .= "<td>". $rank['RankName'] ."</td><td>". $user['PrivilegeID'] ."</td><td>";
            $user_table .= "<td><div class='btn-group'>
                                  <button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>
                                    Action <span class='caret'></span>
                                  </button>
                                  <ul class='dropdown-menu' role='menu'>
                                    <input type='hidden' name='delete-identifier' value='". $user['ID'] ."'>
                                    <li><a name='delete-link' href=''>Delete</a></li>
                                  </ul>
                                </div>
                            </td><tr>";
        }
        $user_table .= "</table>";
        echo($user_table);
    }
?>

Any ideas?

Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54
Cludas18
  • 65
  • 3
  • 12
  • With seeing code, its difficult to tell, – Zee May 30 '15 at 19:48
  • Link: `Delete`, Javascript: `$("a[name='delete-link']").click(function(e){ e.preventDefault(); var ID = $(this).parents("ul").find("input[name='delete-identifier']").val(); $.ajax({ type: 'POST', url: '../ajax.php', data: {delete: 'true', delete_id: RankID}, success: function(response){ $("#result").html(response).fadeIn(500).delay(5000).fadeOut(500); $("div[name='update']").load("ranks.php #update"); } }) })` – Cludas18 May 30 '15 at 19:50
  • Looks extremely easy to read ^^ – Cludas18 May 30 '15 at 19:51
  • Perhaps edit your original question? – Alex McMillan May 30 '15 at 19:52
  • I have made an edit to the question, just waiting for Cludas to accept the edit. – NewToJS May 30 '15 at 19:54
  • Question has been edited with code – Cludas18 May 30 '15 at 19:58
  • by any chance that div with name update contains above a tag? – Chaitanya Gadkari May 30 '15 at 20:02
  • Duplicate Question: [jQuery click not working for dynamically created items](http://stackoverflow.com/questions/9484295/jquery-click-not-working-for-dynamically-created-items) – Yogi May 30 '15 at 21:35

1 Answers1

5

after ajax you are updating the div, .click() do not work on dynamic loaded html. Use .on()

$(document).on("click","a[name='delete-link']",function(e){
    e.preventDefault();
    var ID = $(this).closest("ul").find("input[name='delete-identifier']").val();

    $.ajax({
        type: 'POST', 
        url: '../ajax.php',
        data: {delete: 'true', delete_id: ID},
        success: function(response){
            $("#result").html(response).fadeIn(500).delay(5000).fadeOut(500);
            $("#update").load("test.php");
        }
    })
})

In place of $(document) you can use Id selector of parent of dynamic content as $('#updateDivParentId')

Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54