mardi 29 mai 2018

remove appended data if checkbox unchecked

I want to append data if checkbox is checked and remove appended data if checkbox is unchecked. Dynamic html list is generated as following

<label class="left col20">
  <input type="checkbox" id="newscat[]" name="newscat" value="<?php echo $row['cid']?>">
  <?php echo $row['title']?>
</label>

Data is appended as following when checkbox is checked. I want to remove appended this data or div after checkbox is unchecked.

$(document).ready(function(){
    $('input[name=newscat]').on('click', function(event) {
      if($('input[name=newscat]:checked').length) {
            var menuid = $(this).val();
            $.ajax({
                url:"submenufetch.php",
                method:"POST",
                dataType:"text",
                data:{menuid:menuid},
                success:function(data){
                    $('.submenu').append(data);
                }
            });
        }
        if(!$('input[name=newscat]:checked').length) {
            $('.submenu').append('');
            // need to remove appended element if this checkbox is unchecked
        }
    });
})




Aucun commentaire:

Enregistrer un commentaire