0

I am loading the page (contains the javascript), and then I am loading the PartialView (contains the table). The problem is, I can't select the table id or the onClick event does not fire.

Index.cshtml

$('#sortingRow').on('click', 'th a', function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        data: { sortOrder: sortOrder },
        success: function (result) {
            $('#sortingRow').parent().html(result);
            LoadSampleDesign();
        }
    });
    return false;
});

_PartialView.cshtml

<table>
    <tr id="sortingRow">
        <th>
            <a href="link">Click Me</a>
        </th>
    </tr>
</table>

That partialView is being loaded using Ajax, like this:

$(sections).on('click', '#sectionPageLinks a', function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        success: function (result) {
            $('#sectionPageLinks').parent().html(result);
            LoadSampleDesign();
        }
    });
    return false;
});

I've tried JsFiddle and the javascript seems to be working correctly. Also, At first, you might think the problem is that jquery couldn't find the table since it was being loaded after DOM completed, but that's why I'm using "on()", doesn't that make it live so it should be able to find the #table even after DOM was completed.

4
  • 1
    Is the event handler being called after the partial loads? If not, the events can't be attached to elements that was not rendered in the dom yet. Commented Jun 24, 2014 at 18:28
  • On success function of the load, I've attempted this: $('#sortingRow').bind('click'); Commented Jun 24, 2014 at 18:31
  • When you specify your handler here: $('#sortingRow').on('click', 'th a', are you certain that #sortingRow has been loaded into the DOM at that point? It needs to exist, the th a elements do not. Commented Jun 24, 2014 at 18:33
  • If I got it right, the first code snippet should be called after(its means in the success) of the last code snippet. Commented Jun 24, 2014 at 18:34

1 Answer 1

1

After loading the partial view via Ajax the click event is no longer bound. Change your row click event as follows.

$('#sortingRow').on('click', 'th a', function () {

$(document).on('click', '#sortingRow th a', function () {
Sign up to request clarification or add additional context in comments.

Comments

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.