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

212
Views
Do Something when a Child Element is Not Present

I'm trying to remove a toggle function from the page when a childElement is not present. This has to be done in vanilla javascript. Unfortunately, can not use jQuery. Here's what's going on.

when I run this in the console

let content = document.getElementById('productItemRates-1');
console.log(content.firstElementChild);

Code snippet is returned for checking if childElement is present:

    <div class="product-rate-card">
      <div class="product-rate-card-title">  
        <strong>Clean Power</strong>
        <button type="button" class="card-btn-plain card-tooltip" data-toggle="modal" data-target="#PK1" data-original-title="" title="">
        </button>
      </div>
    </div>

Which is the firstElementChild: div.product-rate-card

What I'm trying to do: Define logic to only remove the toggle button from all locations on the page (not specific to element) if the specific firstElementChild: .product-rate-card is not present in the ParentID: productItemRates-1. There are other references of the childElement: .product-rate-card found in other parentsIDs within the page. Also, order logic wont work because other childElements can take it's place when it is not present.

Used the below code to test removal, that worked, just can't figure out the conditioning logic.

document.querySelectorAll(".product-rates-toggle").forEach(el => el.remove());

Appreciate any guidance..

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

0

// replace document to content
if (!document.querySelector(".product-rate-card")) {
    for (const el of document.querySelectorAll(".product-rates-toggle")) {
        el.remove();
    }
}

about 4 years ago · Juan Pablo Isaza Report

0

Based on a comment of yours:

const toggleratecard = document.querySelector("#productItemRates-1"); 
if (toggleratecard.querySelector('.product-rate-card') === null) {
  toggleratecard.querySelectorAll(".product-rates-toggle").forEach(el => el.remove()); } 
}

Unfortunately your question does not offer any information as to where the toggle is that needs to be removed. My answer assumes it's in the toggleratecard. You can replace that with document if all the toggle on the whole page need to be removed.

Also, from what you write, it isn't entirely clear if the element, if present, must be the first element child.

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!