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

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

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 Denunciar

0

You can write the parents class before the child

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

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