Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Problem solved: Recursive jQuery-AJAX Click Bind event

Hello Friends,

If you are working with jQuery-Ajax, than this might be useful tutorial for you. While using jQuery-Ajax, you will get a response and you will place that response (May be in form of HTML) in some div or table. Now if you want to do any jQuery event on response (for e.g. if you need to make a click event on <a href=””></a> link which is in response) than you need to bind this click event success area of jQuery-Ajax call. You can do this once but again when you receive response, you need to make bind event. So you can say this is recursive jQuery-Ajax call.  So for this you need to make a separate function and call that function recursively. Look at below code.

$(‘.classname’).click(getData);
function getData()
{
id_clicked = $(this).attr(‘id’);
// process here
$.ajax({
url: url here,
success: function(data) {
$(“#desti_id”).html(data);

if(data != ”)
{
$(‘.classname’).click(getData);
}
}
});
}

Let me know if you find any problem.

Share:

3 Comments

  • Birkenstock 37…

    […]Problem solved:Recursive jQuery-AJAX call | Programming Facts -PHP Freelancer-Indian Freelancer[…]…

  • Luis

    Hi…
    I apply this solution but i have a problem. This is my code:

    Coment

    var code = 0;
    function anchorClick(e){
    e.preventDefault();
    code++;
    liid = $(this).attr(“id”);
    $.ajax({
    type: “GET”,
    url: “file.php”,
    data: “id=” + code,
    error: function(jqXHR, textStatus, errorThrown){alert(textStatus);},
    success: function(returnedData){
    $(“.content”).append(returnedData);
    $(“#cmt”).click(anchorClick);
    }
    });
    }

    $(“#cmt”).click(anchorClick);

    file.php:
    <?
    echo "a”;
    ?>

  • I enjoy reading a post that can make men and women think.
    Also, thanks for permitting me to comment!

Leave a Reply

Your email address will not be published.