I have a list in which I have links, on the clicks of those links I am doing stuff. Those links can be added dynamically. For example, I have a input form on the page and when the user fills that form I update the list by appending a new element into the list.
Here is the code for my list
<section id="mealPerDay">
@foreach (MealPerDay mpd in Model.GetMealPerDay())
{
<div class="panel radius @String.Concat("panel_", mpd.Id)">
<span>@mpd.Name</span>
<a class="right edit-margin-left edit showHideEditDailyMeal" data-id="@mpd.Id" data-container="mealPerDay" data-cancel=" ">edit</a>
</div>
}
</section>
When the page loads user sees this list and click function is called like this
$('.showHideEditDailyMeal').click(function (event) {
event.preventDefault();
var container = $(this).data('container');
var id = $(this).data('id');
var cancel = $(this).data('cancel');
if (cancel == ' ') {
$('#' + container + ' div.panel.panel_' + id + ' a.cancel-edit').show();
$('#' + container + ' div.panel.panel_' + id + ' a.edit').hide();
$('#' + container + ' .edit-form.edit_form_' + id).show();
} else {
$('#' + container + ' div.panel.panel_' + id + ' a.cancel-edit').hide();
$('#' + container + ' div.panel.panel_' + id + ' a.edit').show();
$('#' + container + ' .edit-form.edit_form_' + id).hide();
}
});
This works fine, now if I add something, I append it to the list like this
function newGenerateHtml(result, container, classNameForDelete, classNameForEdit, classNameForShowHideEdit) {
var html = '<div class="panel radius panel_' + result.id + '">';
html += '<span>';
html += result.name;
html += '</span>'
html += '<a class="right edit-margin-left edit ' + classNameForShowHideEdit + '" data-id=' + result.id + ' data-container=' + container + ' data-cancel=" ">edit</a>';
html += '</div>';
return html;
}
The mark up I get is similar to the data which load when the page loads. But the click event on the dynamically added data is not working.