This is because #chk_select_abst is not rendered at the time of DOM ready when you are binding that event.
For binding events on dynamically added DOMs, you can bind or delegate the event somewhere on its parent DOM node which you are sure will be there on page load and will NEVER be removed/replaced dynamically. Behind the scenes, this make use of event bubbling or event propagation. Check out this link for more info about bubbling.
A safe bet is to use the parent node as body. But, you can and should use a lower parent node (in this case #ul_AbandonedStates) for performance gain.
Documentation for jQuery event delegation.
var json = {
abandonState: [{
Name: 'name 1',
Value: 'value 1'
}, {
Name: 'name 2',
Value: 'value 2'
}]
}
function addCheckBox() {
if (json) {
if (json.abandonState) {
var abandonedStates = json.abandonState;
var li = $('<li>');
var select = $("<label><input id='chk_select_abst' type='checkbox' class='abandonstatecheckclass' value='-1' name='Select All' /> Select All</label>");
var res = li.append(select);
$('#ul_AbandonedStates').append(res);
$.each(abandonedStates, function(index, item) {
var li = $('<li>');
var chk = $("<label><input id='chk_" + item.Value + "' type='checkbox' class='abandoncheckclass' value='" + item.Value + "' name='" + item.Name + "' />" + item.Name + "</label>");
var res = li.append(chk);
$('#ul_AbandonedStates').append(res);
});
var li = $('<li>');
var btn = $('<button id="ul_AbandonedStates_ok" class="btn btn- primary btn- xs" type="button" value="OK">OK</button>');
var res = li.append(btn);
$('#ul_AbandonedStates').append(res);
}
}
}
$(document).ready(function() {
//change here
$("#ul_AbandonedStates").on("click", "#chk_select_abst", function() {
$("#ul_AbandonedStates input:checkbox").prop('checked', $(this).prop('checked'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<th align="center" width="13%" style="position:relative;" index=6 name="abandonedState">
Abandon State
<div class="down-arrow-wrapper">
<a href="#" class="down-arrow" id="da-abandonstate"></a>
<ul class="checkbox-list" id="ul_AbandonedStates"></ul>
</div>
<th>
</table>
<button class="add" onclick="addCheckBox()">Add List</button>
bodyinstead, that way you will catch the event after DOM change:$("body").on("click", "#chk_select_abst", function () { ... });