Skip to content
Advertisement

when a box is unchecked it clears the previously populated text from checkbox

how do I clear the previously populated text from the checkbox when I uncheck the box?

the list below codes is when you check a box and populated text in the text area.

$('input:checkbox').click(function(){
    var tb = "#"+$(this).attr('rel');
    if($(this).is(":checked"))
        $(tb).append(this.name + "n");

        else($(tb)).removeAttr(":checked");
})

Advertisement

Answer

You can use the replace() function to remove the name from the text field.

$('input:checkbox').click(function() {
  var tb = "#" + $(this).attr('rel');
  let text_to_add = this.name + "n";
  if ($(this).is(":checked")) {
    $(tb).append(text_to_add);
  } else {
    let remove = this.name + "n";
    $(tb).text(function(i, text) {
      return text.replace(text_to_add, '');
    });
  }
})
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement