Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda