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

180
Visualizações
Get id of child elements

I need to get the id from all the child elements, which are in the parent class = "demo", but if the parent class = "demo" has id = "simone" I don't need to get the child id. How to achieve this?

If is simone, don't get children's id, else get children's id?

       var name;
     var divsname = document.querySelectorAll('.demo *');
     divsname.forEach(function(div){
        
        if (div.hasAttribute("id")) {
        name += "Name: "+div.getAttribute("id")+", "; 
        }else{
            
        }
        document.getElementById("get-demo").innerHTML = name;
    console.log(name);
     });
<div id="maja" class="demo"><div id="maja-child"></div></div>
<div id="alex" class="demo"><div id="alex-child"></div></div>
<div id="simone" class="demo"><div id="simone-child"></div></div>
<div id="pit" class="demo"><div id="pit-child"></div></div>

<div id="get-demo"></div>

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

0

You can use the css :not selector and exclude the id simone.

       var name;
     var divsname = document.querySelectorAll('.demo:not(#simone) *');
     divsname.forEach(function(div){
        
        if (div.hasAttribute("id")) {
        name += "Name: "+div.getAttribute("id")+", "; 
        }else{
            
        }
        document.getElementById("get-demo").innerHTML = name;
    console.log(name);
     });
<div id="maja" class="demo"><div id="maja-child"></div></div>
<div id="alex" class="demo"><div id="alex-child"></div></div>
<div id="simone" class="demo"><div id="simone-child"></div></div>
<div id="pit" class="demo"><div id="pit-child"></div></div>

<div id="get-demo"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use @jkoch answer or if you need to do something with any div with id 'simone' you can filter it within the forEach callback function. The name variable I set to a blank "" to avoid getting 'result' in the name variable.

var name = ""
     var divsname = document.querySelectorAll('.demo');
     divsname.forEach(function(div){
        
        if (div.id !== 'simone') {
        name += "Name: "+div.id+", "; 
        }else{
            //code if you want to do something with simone id
        }
        document.getElementById("get-demo").innerHTML = name;
            console.log(name);
     });
about 4 years ago · Juan Pablo Isaza Relatório

0

This should work.

document.querySelectorAll(".demo").forEach((item) => {
  if (item.id !== "simone") {
    console.log(item.firstChild.id);
  }
});
    <div id="maja" class="demo"><div id="maja-child"></div></div>
    <div id="alex" class="demo"><div id="alex-child"></div></div>
    <div id="simone" class="demo"><div id="simone-child"></div></div>
    <div id="pit" class="demo"><div id="pit-child"></div></div>

    <div id="get-demo"></div>

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