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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!