0

I have a table created in javascript. On each row i am adding a checkbox. On checking the checkbox a warning message has to be displayed. how to add the oncheck attribute to a dynamic checkbox in javascript.

var tblRow = "<tr><td Style='display: none'>" + data.IxDetails
        "</td><td>" + data.Name +
             "</td><td>" + GroupTable +
              "</td><td>" + (data.AllowFileAttachment == true ? "Yes" :"No" )  +
        "</td><td>" + (data.SetToInActive == true ? "INACTIVE" : "ACTIVE" ) +
              "</td><td><input type = 'checkbox' id='" +
       data.Index + "' name='selectedData'  value='123' "+
        "</td><td><input type = 'checkbox' id='dl" +
       data.Index + "' name='selectedData'  value='123' >" +
        "</td></tr>";
        $('#DataTable> tbody:last').append(tblRow);

5 Answers 5

4

Try me:

 $('table tr').find('input:checkbox').on('click', function(){
         alert("I'm clicked!");
 });    

SIMPLEST DEMO FIDDLE

Sign up to request clarification or add additional context in comments.

5 Comments

You dont have to use find() but place 'input:checkbox' as a 2nd parameter on your on() instead.
SIMPLEST DEMO FIDDLE. nice. hehehe. alert("I'm clicked") vs alert('just clicked!');
@VondRitz but it will not work on dynamically added checkboxes. Read event delegation here: api.jquery.com/on :)
and remove tr from $('table tr').. =)
But how to differentiate between the two checkboxes
2

Since the checkbox is added dynamically, you need to use event delegation to register the event handler.

// New way (jQuery 1.7+) - .on(events, selector, handler)
$('#DataTable').on('click', ':checkbox', function(event) {
    event.preventDefault();
    alert('testlink'); 
});

This will attach your event to any checkbox within the #DataTable element, reducing the scope of having to check the whole document element tree and increasing efficiency.

A SIMPLE WORKING FIDDLE DEMO

Comments

1
var tblRow = "<tr><td Style='display: none'>" + data.IxDetails
        "</td><td>" + data.Name +
             "</td><td>" + GroupTable +
              "</td><td>" + (data.AllowFileAttachment == true ? "Yes" :"No" )  +
        "</td><td>" + (data.SetToInActive == true ? "INACTIVE" : "ACTIVE" ) +
              "</td><td><input type = 'checkbox' onchange='yourfunction()' id='" +
       data.Index + "' name='selectedData'  value='123' "+
        "</td><td><input type = 'checkbox' onchange='yourfunction()' id='dl" +
       data.Index + "' name='selectedData'  value='123' >" +
        "</td></tr>";
        $('#DataTable> tbody:last').append(tblRow);

Comments

1

for dynamically added controls better use

<input type = 'checkbox' id='dl" +
       data.Index + "' name='selectedData' href="javascript:void(0);" onclick="dynaclick('id1')"  value='123' >

and javascript function is

function dynaclick(id){
alert('checkbox with'+id+' is clicked');

}

Comments

0

For dynamically generated content you could use on

$("form").on("change", function(event) {
  alert('Checkbox click!');
});

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.