My regular onclick event, like so works:
$(document).ready(function () {
$("#guestlist .viewguestdetails").click(function () {
$(".guestdetails[data-id='18']").toggle();
});
});
However, when that onclick event is on dynamic HTML, a row I'm inserting into a table, it no longer works. As in: nothing happens. So I checked out this post: Event binding on dynamically created elements?
And as answered in there went for .on() method.
I added a class newrow to this tr element and now have this:
$(document).ready(function () {
$('.newrow').on('click', 'span', function () {
$(".guestdetails[data-id='" + $(this).attr('data-id') + "']").toggle();
});
});
Here I'm building a new row and inserting that:
var newrow='';
newrow += '<tr class="newrow" data-id="' + id + '">';
newrow += '<td class="alignleft sorting_1"><span class="viewguestdetails" data-id="' + id + '">' + firstname + ' ' + lastname + '</span>';
newrow += ' <span class="completeguestdetails" data-id="' + id + '">(' + $("#completedetails").attr('data-message') + ')</span><br/>';
newrow += '<div class="guestdetails" data-id="' + id + '">';
newrow += SOME DATA';
newrow += '</div>';
newrow += '</td>';
//add checkboxes
var checkstatus = 'off';
newrow += '<td><span class="attendance checkbox-' + checkstatus + '" data-id="' + id + '" data-daypart="day"></span></td>';
newrow += '<td><span class="attendance checkbox-' + checkstatus + '" data-id="' + id + '" data-daypart="ceremony"></span></td>';
newrow += '<td><span class="attendance checkbox-' + checkstatus + '" data-id="' + id + '" data-daypart="reception"></span></td>';
newrow += '<td><span class="attendance checkbox-' + checkstatus + '" data-id="' + id + '" data-daypart="diner"></span></td>';
newrow += '<td><span class="attendance checkbox-' + checkstatus + '" data-id="' + id + '" data-daypart="party"></span></td>';
newrow += '</tr>';
//add the row to the guestlist table
$('#guestlist tbody').prepend(newrow);
I used jQuery debugger (https://chrome.google.com/webstore/detail/jquery-debugger/dbhhnnnpaeobfddmlalhnehgclcmjimi) and found that on the inserted row or on the span tags inside it, there are no jQuery events attached.
What am I missing?
UPDATE I added this and moved it outside the document.ready() function. In that case the events are attached:
$('#guestlist').on('click', 'span.viewguestdetails', function () {
$(".guestdetails[data-id='" + $(this).attr('data-id') + "']").toggle();
});
But then the already existing rows no longer work. May that be because I still have this in my document.ready():
$("#guestlist .viewguestdetails").click(function () {
$(".guestdetails[data-id='" + $(this).attr('data-id') + "']").toggle();
});
So, right now, either the newly inserted row functions correct OR the already existing rows function correct.
Why?
` tag. (for debugging purposes). Oterwise when the code is outside the .ready() function and before the html has been read It will not work because the elements of the DOM are not there yet.
– Sergio Sep 16 '13 at 19:13