Alex Mathew
Alex Mathew

Reputation: 1554

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 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?

Upvotes: 3

Views: 688

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You should delegate event handling with on() so that everything works also on newly added elements (jQuery > 1.7);

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

if you use an older version of jQuery use delegate or live

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

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

Upvotes: 1

Starx
Starx

Reputation: 78981

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);
});

Upvotes: 0

Related Questions