Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda