Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
remove li element when checkbox is unchecked

I am dynamically able to add a new li element on checked event of a checkbox. However I am not able to remove the same on the unchecked event.

$(document).ready(function() {

    var value;

    $(".tmCheckbox").change(function() {
        if (this.checked) {
            value = $(this).val();
            $('.ul_sec').append($('<li>', {
                text: value
            }));
        } else {
            // $(this).parent().remove();
        }

    });
});

HTML CODE:

<h3>MY SELECTION </h3>
<ul class="ul_sec" style="display: block;">
    <li><input type="checkbox" name="filter" value="">Random Text 1</li>
    <li><input type="checkbox" name="filter" value="">Random Text 2</li>

</ul>

<h3>FILTER</h3>
<ul style="display: block;">
    <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 1">Random Text 1</li>
    <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 2">Random Text 2</li>
    <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 3">Random Text 3</li>
</ul>
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Filter out li element based on text content using filter() method and remove it using remove() method.

var $this = this;
$('.ul_sec li').filter(function(){
    return $(this).text() == $this.value;
}).remove();

$(document).ready(function() {

  var value;

  $(".tmCheckbox").change(function() {
    var value = $(this).val();
    if (this.checked) {
      $('.ul_sec').append($('<li>', {
        html: this.outerHTML + value
      }));
    } else {
      $('.ul_sec li').filter(function() {
        return $(this).text() == value;
      }).remove();
    }

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>MY SELECTION </h3>
<ul class="ul_sec" style="display: block;">
</ul>

<h3>FILTER</h3>
<ul style="display: block;">
  <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 1">Random Text 1</li>
  <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 2">Random Text 2</li>
  <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 3">Random Text 3</li>
</ul>

about 4 years ago · Santiago Trujillo Report

0

Just use remove() to the target where it is appended

Like this:

$(document).ready(function() {

    var value;

    $(".tmCheckbox").change(function() {
        if (this.checked) {
            value = $(this).val();
            $('.ul_sec').append($('<li>', {
                text: value
            }));
        } else {
             $('.ul_sec li').last().remove();
        }

    });
});
about 4 years ago · Santiago Trujillo Report

0

document.getElementById("yourid").remove(); giving an id to the <li> you just added.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!