1

Brain ache.

I have a timer that dynamically adds row to a table. I want an event to fire when the user clicks on one of the dynamically added rows.

I'm tagging each for with a class of "PortletTableUseButton" and using the .on("click") but it doesn't work.

    $('.PortletTableUseButton').on("click", function () {
        alert($(this));
    });
    setInterval(function () { addRow(); }, 2000);

    var counter = 2042;
    function addRow() {
        counter++;
        var myRow = "<tr class='PortletTableUseButton'><td>BLOCK-" + counter + "</td><td>Location A</td><td >Use</td></tr>";

        $("#Portlet #content tr:last").after(myRow);
    }
<div id="Portlet" style="position:absolute; top:500px;left:500px; height:200px;">
    <div id="content" style="height:80%;">
        <table id="PortletTable">
            <tr>
                <td>File Name</td>
                <td>Location</td>
                <td>Action</td>
            </tr>
        </table>
    </div>
</div>

1 Answer 1

4

You aren't using event delegation correctly. Try this:

$(document).on('click', '.PortletTableUseButton', function () { ... });

http://learn.jquery.com/events/event-delegation/

Alternatively, run your click function each time you add a row.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but no change. The event doesn't get triggered.
Sure it does. Are you seeing any syntax or other errors in the console? What does your HTML look like after a row has been added?
Thank you sir. you were right: you're code was correct. Thanks for the help: my brain's not aching so much now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.