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

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

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 Denunciar

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