JQuery based checkbox not working

$(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 that I used every time I check a checkbox in the class list. I am getting a new one in the #regTitle

div, but the problem I am facing is the newly created checkboxes that cannot be checked, can you guys tell me what the problem is?

+3


source to share


2 answers


You must delegate event handling with on()

so that everything works the same on newly added elements (jQuery> 1.7);

$("body").on("change", ".list", function()
{
$("#regTitle").append(htmlnew);
 });

      



if you are using an older version of jQuery or live delegate

$(document).delegate(".list","change", function()
{
$("#regTitle").append(htmlnew);
 });

$(".list").live("change", function()
{
    $("#regTitle").append(htmlnew);
});

      

+1


source


Your checkbox change event does not bind to dynamically generated elements. You will have to delegate the binding. Using .on () is very good for this purpose.



$("body").on("change", ".list",function() {
    $("#regTitle").append(htmlnew);
});

      

0


source







All Articles