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

271
Views
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 answers
Answer question

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 Report

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 Report

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 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!