Dynamic button click not working.
In simple terms, I have a hidden template that contains some text and a button.
<div id="itemContainer" class="itemColumn">
<div class="itemDiv" id="items">
- <ul />
</div>
</div>
<ul class="hiddenTemplate">
<li id="TemplateListItem" class="release">
<div class="releaseDetailDev">
<p class="title">Title: </p>
<p class="itemTitle"></p>
<div class="buttons">
<button type="button" class="editButton">Edit</button>
</div>
</div>
</li>
</ul>
In a loop function I am cloning the #TemplateListItem, adding data to the title. And then I want to add a click event that has a different variable for each button. My code is as follows.
function addItemToList(currentItem) {
//create a copy of the <li> template
var itemTemplate = $('#TemplateListItem').clone();
//remove the unnecessary id attribute
itemTemplate.attr('id', null);
//set the values
itemTemplate.find('.Title').text(currentItem.get_item('Title'));
var id = currentItem.get_item('ID');
- itemTemplate.data('id', id);
- itemTemplate.find('.editButton').live("click", function () {
- OpenEditDialog(id);
- });
- var list = $('.itemDiv>ul');
- list.append(itemTemplate);
- }
- function OpenEditDialog(id) {
- alert("Id = " + id
- }
This does sucessfully display a list of items with buttons. So I have items with Id's 1 - 5. No matter which button I click I will get 5 pop ups one after another saying
Id = 1
Id = 2
Id = 3
Id = 4
Id = 5
How can I ensure that when I hit a given button it only gives me the alert for the button I've clicked and no other alerts afterwards (unless I click the given button of course)?