Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery trigger event for checkbox

I've series of checkbox, for which I've written jquery script on click. When user clicks on checkbox it opens another series of checkbox and it goes on. Now my problem is when users checks few of those checkbox, and moves to next page and for some reason he/she comes back to checkbox page. Then I should have all the boxes which he/she has checked to open up. I've written a check box test when dom loads

$(document).ready(function(){
   if($('.main').is(':checked')){
      $(this).trigger('click'); 
   }else{
      //do  nothing
   }

  $('.main').click(function(){
      if($('.main').is(':checked')){
       var val = $(this).attr('alt');
       $('#'+val).show();
   }else{
       var val = $(this).attr('alt');
       $('#'+val).hide();
   }
  });
});

As you see above. Main is the checkbox class. On click of that, am opening another series of checkbox. Now I want this to done automatically for their checked boxes when they visit the page again

like image 862
Saravanan Sampathkumar Avatar asked Oct 21 '22 11:10

Saravanan Sampathkumar


1 Answers

use session or cookie to save .main class checkbox values and checked those accordingly when you come back and then use the following code:

<script type="text/javascript">
$(document).ready(function(){

   $('.main').each(function(){
      if($(this).is(':checked')){
        var val = $(this).attr('alt');
        $('#'+val).show();
      }else{
        var val = $(this).attr('alt');
        $('#'+val).hide();
       }
     });


   if($('.main').is(':checked')){
      $(this).trigger('click'); 
   }else{
      //do  nothing
   }

  $('.main').click(function(){
      if($('.main').is(':checked')){
       var val = $(this).attr('alt');
       $('#'+val).show();
   }else{
       var val = $(this).attr('alt');
       $('#'+val).hide();
   }
  });
});
</script>
like image 66
saifur Avatar answered Oct 27 '22 11:10

saifur