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

111
Visualizações
How Do I Access An Element from Within Another Element?

I'm building a website and I need to access an element with a specific class name from another element but I have used this class name within other groups.

    <div class="1">
Object 1
<div class="hair">Black</div>
</div>
<div class="2">
Object 2
<div class="hair">Brown</div>
</div>
<div class="3">
Object 3
<div class="hair">Red</div>
</div>    

How would I access the hair for solely Object 3?

Could someone please provide a solution? Thank you so much!

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

0

If you need to access this element with CSS, you can simply use the parent class.

.3 .hair{}

If you are trying to get this object in JS, you have two choices. The first one is to use vanilla js.

//+ES6
document.querySelector(".3 .hair").innerText;
//Red

//---------

//vanilla js
document.getElementsByClassName("3")[0].getElementsByClassName("hair")[0].innerText;
//Red

The second choice is jQuery. It's like using the query selector.

$(".3 .hair").text();
//Red
about 4 years ago · Juan Pablo Isaza Relatório

0

You can write the parents class before the child

const el = document.querySelector(".3 .hair")
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the parent class as they are unique:

.three > .hair

Please Note:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit

Demo:

let thirdEl = document.querySelector('.three > .hair');
console.log(thirdEl.textContent)
<div class="one">
  Object 1
  <div class="hair">Black</div>
</div>
<div class="two">
  Object 2
  <div class="hair">Brown</div>
</div>
<div class="three">
  Object 3
  <div class="hair">Red</div>
</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