1

supposing I have 5 checkboxes that I created manually through HTML, all of which have the attributes; Name, Type and Value. All of those checkboxes, when checked triggers an event which allows a button to be enabled. (Code below)

    $('input[type="checkbox"]').click(function() {
        var $this = $(this);
        if ($this.is(':checked')) {
            $("#deleteModalTrigger").prop("disabled",false);
        } else {
            $("#deleteModalTrigger").prop("disabled",true);
        }
    });

Now, I create another checkbox through javascript and append it to a row in a table. It appears in the view, but checking it does not trigger the code above even though it is also a checkbox.

            var column1 = document.createElement('td');
            var cb1 = document.createElement('input');
            cb1.type="checkbox";
            cb1.name="foo";
            cb1.value="Something-cbvalue-0";
            column1.setAttribute("rowspan","2");
            column1.appendChild(cb1);
            row.appendChild(column1);

Any help is appreciated! Thank you.

2 Answers 2

4

as the checkbox is added dynamically after the dom is loaded

$('input[type="checkbox"]').click(function() {

this click event is not binded to the newly added element try using

$(document).on('click','input[type="checkbox"]',function() {
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir! That worked :) SO says I can't accept your answer as correct for 11 more minutes. I will mark it as correct once the time is up. Thank you again!
0

You Can use the on function to get the change event of newly created checkbox. Inside that function you can check the checked property and make the target button as enabled or disabled.

$(document).on('change','yourCheckboxID',function(){

    if($(this).prop('checked'))
      {

            //Disable or enable the button

      }

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.