Mona Coder
Mona Coder

Reputation: 6316

Using checkbox on Bootstrap 3 tab

Using Bootstrap 3 and jQuery I have a checkbox like this

 var eachrow = "<tr>" + "<td align='center'><input type='checkbox' class='case' name='case[]' value='1' type='checkbox'></td>"

 + "<td>Test</td>" + "</tr>";


 $('#compValues').append(eachrow);

$("input:checkbox[name='case[]']").change(function(){
    if($(this).is(':checked')){

      alert("I am checked");
    }
    else{
         alert("I am Unchecked");
        }
});

Now the check box can be marked (checked or un-checked but the .change() function not doing any thing! no error or something else. Why is this happening?

Upvotes: 0

Views: 354

Answers (1)

ChaoticNadirs
ChaoticNadirs

Reputation: 2290

To attach an event handler to an element that is created dynamically you can use delegated events http://api.jquery.com/on/

example:

$("table").on("change", "input:checkbox[name='case[]']", function() {
    if($(this).is(':checked')){
        alert("I am checked");
    }else{
        alert("I am Unchecked");
    }
});

Upvotes: 1

Related Questions