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

279
Views
jQuery remove element with next() and remove()

I am using jQuery to remove an element with the class named "added" There are multiple elements with this class name.

$(".steps-row-first").on("change", ".anotherCheese", function() {
    if($(this).val() == 'no') {
         $(this).parent().next(".added").remove();
    }
    else
    {
         $(".steps-row-first").append("<div class='added'></div>");
    }
});

Here is the HTML:

<div class="steps-row-first">
   <div class="form-group">
      <div class="radio-wrapper">
         <div class="radio-group">
            <input type="radio" class="anotherCheese" name="anotherCheese" value="yes">
            <label>Yes</label>
         </div>
         <div class="radio-group">
            <input type="radio" class="anotherCheese" name="anotherCheese" value="no">
            <label>No</label>
         </div>
      </div>
   </div>
   <div style="clear:both;"></div>
   <div class="added"></div>
</div>

When I click the no radio button the element does not get removed, what am I doing wrong ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Your jQuery is wrong for removing the element. In $(this).parent().next(".added").remove();, $(this) is the input element, so it's parent is just the .radio-group element. You need to change it to $(this).parents('.radio-wrapper').next(".added").remove();

Even above will not work because there is a <div> between .radio-wrapper and .added, to make it work, you need to use next() twice: $(this).parents('.radio-wrapper').next().next(".added").remove();

about 4 years ago · Juan Pablo Isaza Report

0

I dont know , where what's exactly the .steps-row-first the element

but try using parents to get back then chose the parent's siblings to target your div with added class as below:

....
if($(this).val() == 'no') {
     $(this).parents(".form-group").siblings(".added").remove();
}
....
about 4 years ago · Juan Pablo Isaza Report

0

$(".steps-row-first").on("change", ".anotherCheese", function() {
    if($(this).val() == 'no') {
         $(".steps-row-first").find(".added").remove();
    }
    else
    {
         $(".steps-row-first").append("<div class='added'></div>");
    }
});

you can use find() to find all class .added and remove it

about 4 years ago · Juan Pablo Isaza 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!