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

168
Visualizações
Jquery: Give styling (padding) of the parent class based on the condition that the first child matches with the class I want

Parent Class: cmp-container There are two child classes - productmodelfootnotes footnotes

According to the sequence of the child I want to give padding as follows - If "productmodelfootnotes" child class comes first -> Apply padding=40px to the parent class (cmp-container) If "footnotes" child class comes first -> Apply padding=64px to the parent class (cmp-container)

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

0

First of all, initialize your parent element

let parent = document.querySelector('.cmp-container')

Then add style, based on condition

let firstChild = parent.children[0];
if(firstChild.classlist.contains('productmodelfootnotes'){
    parent.style.padding = "40px";
} else if(firstChild.classlist.contains('footnotes')) {
    parent.style.padding = "64px";
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Consider the following.

$(function() {
  $(".cmp-container").each(function(i, el) {
    if ($("div:eq(0)", el).hasClass("productmodelfootnotes")) {
      $(el).addClass("shortpad");
    } else if ($("div:eq(0)", el).hasClass("footnotes")) {
      $(el).addClass("longpad");
    }
  });
});
.cmp-container {
  border: 1px solid blue;
  margin-bottom: -1px;
}

.cmp-container .footnotes {
  border: 1px solid red;
}

.cmp-container .productmodelfootnotes {
  border: 1px solid green;
}

.shortpad {
  padding: 40px;
}

.longpad {
  padding: 64px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cmp-container">
  <div class="productmodelfootnotes">
    Product Note 1
  </div>
  <div class="footnotes">
    Foot Note 1
  </div>
</div>
<div class="cmp-container">
  <div class="footnotes">
    Foot Note 2
  </div>
</div>
<div class="cmp-container">
  <div class="productmodelfootnotes">
    Product Note 3
  </div>
  <div class="footnotes">
    Foot Note 3
  </div>
</div>
<div class="cmp-container">
  <div class="footnotes">
    Foot Note 4
  </div>
  <div class="productmodelfootnotes">
    Product Note 4
  </div>
</div>

Using .each(), we can iterate over the DIV elements and check the order of the children. If they meet the specific condition, we can add a Class or apply a Style to that element.

See More: https://api.jquery.com/each/

I assign i as the Index and el as the Element. I then use a Find selector, $("div:eq(0)", el) which is the same as $(el).find("div").eq(0) where 0 represents the first item in the list or elements that is Found.

Using .hasClass() we can check if it is true or false, and perform a specific action based on the condition.

See More: https://api.jquery.com/hasClass/

I used an if / else if statement to check two conditions. This is in case another element may appear first that should not have any padding.

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