Hello-World
Hello-World

Reputation: 9545

table ready jquery must fire

I need to make a script fire when "myTable" is on the page but it is rendered later by ajax and I can't edit the ajax. Can I leave this script below on the page and only with the table appears it will fire? How do I do this?

thanks....

<html>
<body>
<script>
    $('#myTable').ready(function(){

        alert('ready');

    });

</script>
</body>
</html>

table rendered later

<table id="myTable">

<tr>
    <td>t</td>
    <td>t</td>
    <td>t</td>
    <td>t</td>
</tr>

    <tr>
    <td>t</td>
    <td>t</td>
    <td>t</td>
    <td>t</td>
</tr>


</table>

Upvotes: 0

Views: 4043

Answers (2)

coderek
coderek

Reputation: 1870

You can setup ajax complete function at the beginning of you scripts and check the existence of the table inside there.

$.ajaxSetup({
    complete: function () {
        if ($("#mytable").length>0) do_something;
    });

Upvotes: 1

valentin_nasta
valentin_nasta

Reputation: 608

You should make a trigger function after the creation of the table.

function create_element() {
    // Create an element
    $('body').append("<p>Testing..</p>")
    // Trigger
    $('body').trigger('elementCreated');
}

$(document).ready(function () {
    $("body").on("elementCreated", function (event) {
        alert('One more');
    });
});

http://jsfiddle.net/GT7eB/

Upvotes: 1

Related Questions