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

279
Visualizações
Removing div with no class and id without removing its children

I am trying to remove div with no id and class <div> without removing its children.

<div class="struc">
..
..
<div class="required-field half-control form-group has-feedback">
  <label for="fxb_d56c971 class=" control-label">First Name</label>
  <input id="fxb_d56c971a class="design_textfield" type="text" value="" placeholder="First Name*">
  <div>
    <span class="field-validation-error" data-valmsg-replace="true">
      <span id="fxb_d56c971a" class="">First Name is required.</span>
    </span>
  </div>
</div>
..
..
</div> 

I tried the following code which is not working

        var findExtraDiv = $(el).prev('label').parent();
        if ($(findExtraDiv).prop("tagName").toLowerCase() == 'div') {
            if (($(findExtraDiv).id == null) && ($(findExtraDiv).attr("class") == "")) {
                var cnt = $(findExtraDiv).contents();
                $(findExtraDiv).replaceWith(cnt);
            }
        }

I can find the correct div but unable to remove the parent div only

This is what I want

<div class="struc">
...    
  <div class="required-field half-control form-group has-feedback">
    <label for="fxb_d56c971 class=" control-label">First Name</label>
    <input id="fxb_d56c971a class=" design_textfield" type="text" value="" placeholder="First Name*">
    <span class="field-validation-error" data-valmsg-replace="true">
      <span id="fxb_d56c971a" class="">First Name is required.</span>
    </span>
  </div>
...
</div>

Any suggestions or help would be appreciated.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try this, please:

$(".struc div").each(function(){
    let temp = $(this).html();
    if($(this).attr('id')===undefined){
        console.log('id is undefined');
        $(this).remove();
        $('.struc').append(temp);
    }
})

Here goes a working example at jsfiddle:

https://jsfiddle.net/vkcds1p0/

about 4 years ago · Juan Pablo Isaza Relatório

0

You code is correct except for two mistakes:

  • $(findExtraDiv).id jQuery has no "id" property. Should be $(findExtraDiv).attr('id') == undefined
  • $(findExtraDiv).attr("class") == "" missing attribute returns 'undefined', not empty string. You should have $(findExtraDiv).attr("class") == undefined

With these two modifications and assuming your "$(el)" is correct, your code will work.

about 4 years ago · Juan Pablo Isaza Relatório

0

Below code can find out all div elements having no attributes and then unwrap it. This will return your requested output.

const noAttributeElems = $("div").filter(function(){ 
    if(this.attributes.length === 0){
        $(this).contents().unwrap();
    } 
});
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