Joshua Tan
Joshua Tan

Reputation: 356

Populate checkbox dynamically and on tick function calls server side

I need to create a table with dynamically populated rows of checkbox. The checkbox list will change according to the company they are in

e.g: 1st company : Apple friends

 Name     email        iPhone   iPad  macbook  macpro 
 john  [email protected]      ckb      ckb     ckb      ckb
 Mel   [email protected]     ckb      ckb     ckb      ckb 

2nd company : Mirco Friends

 Name     email        windows   office 
 jav   [email protected]      ckb      ckb    
 Kel   [email protected]      ckb      ckb   

If that is doable how do i go about creating a method to catch any on change event for the checkboxes?

A normal checkbox change function:

$('#checkbox1').change(function())

But how do we write codes or ids for checkboxes that are defined dynamically?

I know these seems like 2 Questions but the second question is tied with the first.

Appreciate your help

Upvotes: 0

Views: 174

Answers (2)

Se0ng11
Se0ng11

Reputation: 2333

Roughly the idea is, loop through every single checkbox that is checked, and then get the id of the checkbox, if you can get the id, I believe you can easily do anything you want using the id that you got

$(':checked').each(function(){
    var $this = $(this);

    console.log($this.attr('id'));

});

Updated

$(':checkbox').change(function(){
    var $this = $(this);

    console.log($this.attr(id)); // u get the id here if any checkbox is tick/untick

    if ($this.prop("checked")){
        //do something when it is checked
    }else{
        //do something when it is unchecked
    }

});

Upvotes: 1

Arunkumar Vasudevan
Arunkumar Vasudevan

Reputation: 5330

You may Try like

$(document).on('change', '#checkbox1', function() {
alert('alerted');
});

Upvotes: 0

Related Questions