March 15, 2012
jQuery based checkbox not working
Question by Alex Mathew
$(document).ready(function() {
var dataString="hy";
var htmlnew="<input type='checkbox' name='formDoor' value='A' class='list' enable='true'>Check 1";
alert(htmlnew);
$(".list").change(function()
{
$("#regTitle").append(htmlnew);
});
});
The above is which i used when each time i check the checkbox with class list. i get a new one in #regTitle
div, but the problem i am facing is the newly generated check boxes are not able to checked,can you guys tell me whats the problem?
Answer by Starx
Your checkbox’s change event does not attach to the dynamically generated elements. You will have to delegate the bind. Using .on() is very good for this purpose.
$("body").on("change", ".list",function() {
$("#regTitle").append(htmlnew);
});