Sreeni Puthiyillam
Sreeni Puthiyillam

Reputation: 485

How can i loop through a group of check box

var days = ['daily', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];

these are my check box. Every check box have unique id. backup_freq_daily,backup_freq_monday, etc. Whenever i click on daily ,or monday or thu or thu etc, i need to do some operation.

$('#bm_freq_daily,#bm_freq_mon,#bm_freq_tues,#bm_freq_weds,#bm_freq_thur,#bm_freq_fri,#bm_freq_sat,#bm_freq_sun').click(some_operation);

insted of this how can i write a for loop ?

for (var dx = 0; dx < days.length; dx ++ ) {
  $("#bm_freq_daily").click(some_operation);
}

This is not work, i just want to loop through all days[] when ever user clcik check box i loop the array and find which id is clicked.
Thanks in adavnce

Upvotes: 1

Views: 1240

Answers (3)

tibo
tibo

Reputation: 5494

$('#yourParent :checkbox').each(function(){
  $(this).click(some_operation);
});

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15881

you can use .each()method to iterate over it, by adding a css class to your checkboxes.

something like this

$('.yourCheckboxClass:checked').each(function(index, Elem)
{
  if($(this).id=="something")
   { //then do something 
   }
});

Upvotes: 1

4b0
4b0

Reputation: 22321

Use Find.You tag jquery so i try to put a sample in jquery.

var Days= $('#divDays').find('input.YourDayClass:checked');
                    $.each(Days, function(index, item) {
                        if ($(this).attr("checked")) {
                           //What You try to Do
                        }
                    });

I think sample help you.

Upvotes: 0

Related Questions