How to set jquery function for all of <a> element in ajax response

Edison

I have a function with an ajax code to get data from a table filter by username.

function showdata(){
var username  = $("#username").val(); // input:text element id = 'username'
$.ajax({
    type:"POST",
    url:"purchase_process.php",    
    data: "act=show&username=" + username,
    success: function(data){
        $("#show_data").html(data); // div element with id = 'show_data'
        $("#show_data").fadeIn(2000);
    }
});}

showdata();

$(".del_button").click(function(e) {
e.preventDefault();
var id = $(this).attr('id');
var no = id.split('-');
var number= no[1];
var username  = $("#user_"+number).val();
var code  = $("#code_"+number).val();

$.ajax({
    type:"POST",
    url:"purchase_process.php",    
    data: "act=delete&username=" + username + "&code=" + code ,
    success: function(data){                 
        $("#info").html(data);  // div element id = info
        showdata(); 
    }  
}); });

And here is the ajax response from purchase_process.php that show all data in a table that each rows of the data have element show in a image (delete.png) to delete row :

<table id='purchase_table' name='purchase_table' border='0' class='table'>
    <thead>
        <tr><th><center>No.</center></th>
            <th><center>Part Number</center></th>
            <th><center>Part Name</center></th>
            <th><center>Location</center></th>
            <th><center>Qty</center></th>
            <th><center>Cost</center></th>
            <th><center>Subtotal</center></th>
            <th><center>Action</center></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td width=70><center>$no</center></td>
            <td>
                <center>$r[part_code]</center>
                <input type=hidden id='user_$no' name='user_$no' maxlength=20 
                                   value='$_POST[username]' readonly>
                <input type=hidden id='part_$no' name='part_$no' maxlength=20 
                                   value='$r[part_code]' readonly>
            </td>
            <td>$r[part_name]</td>
            <td><center>$r[location]</center></td>
            <td><center>$r[qty]</center></td>
            <td><center>".number_format($r['cost'],2,",",".")."</center></td>
            <td><center>".number_format($subtotal,2,",",".")."</center></td>
            <td width=80><center>
                <a href='#' class='del_button' id='del-$no' name='del-$no'>
                    <img src='img/delete.png'>
                </a>
            </center></td>
        </tr>
    </tbody>
</table>

Function showdata() is running well but jquery function event:click for "a" element with class = "del_button" have no response whenever I click in image (delete.png) However if I make the same "a" element in that page, that jquery function running well. I assumed that jquery function don't get the "a" element value because that was called from ajax response.

Is there anyone can help me to solve this trouble? Thanks before...

Jay Blanchard

jQuery is only aware of the elements in the page at the time that it runs, so new elements added to the DOM are unrecognized by jQuery. To combat that you have to use event delegation, bubbling events from newly added items up to a point in the DOM that was there when jQuery ran on page load. Many people use document as the place to catch the bubbled event, but it isn't necessary to go that high up the DOM tree. As a matter of course you should delegate to the nearest parent that exists at the time of page load.

$(document).on('click', '.del_button', function(e) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set AJAX response to jquery data table columns?

From Dev

jQuery finding element in ajax response, find not working

From Dev

Trigger JQuery event on ajax response element

From Dev

JQuery $.ajax set cookie and get html response

From Dev

JQuery AJAX HTML response in error function

From Dev

Call function in jQuery from AJAX response

From Dev

JQuery AJAX HTML response in error function

From Dev

jQuery Ajax passing response variable to function

From Dev

jQuery get response from ajax function

From Dev

How to replace html element with ajax response?

From Dev

How to append ajax response in the unknown element?

From Dev

how to get an element added from an ajax response

From Dev

How to append ajax response in the unknown element?

From Dev

how to use ajax response in attr of an element

From Dev

How to make middleware for response on all ajax requests

From Dev

How to make middleware for response on all ajax requests

From Dev

How to change all class name in ajax response?

From Dev

how to deselect all element in jquery?

From Dev

how to deselect all element in jquery?

From Dev

How to execute JavaScript function in an Ajax() Response

From Java

How to get the value of Ajax response in jQuery

From Dev

how can I parse ajax response in jquery?

From Dev

how to print json array in jquery with ajax response

From Dev

How to parse asp response with jQuery ajax?

From Dev

how to display jquery loop in ajax response

From Dev

How to split the Response html of ajax in jquery

From Dev

jQuery parse ajax response data and get element id - value

From Dev

Set a delay in a repeating jQuery / Ajax function

From Dev

jQuery set element attribute without altering function

Related Related

HotTag

Archive