Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
get the child inside the parent javascript

I am trying to show the child on hover over the parent or the child itself. so on hover over one div show the div inside it

in other words on hover over the red container or blue div show the blue div

const bgsC = document.querySelectorAll('.cons');
const bgs = document.querySelectorAll('.bgs');

bgsC.forEach(bgc => {
  bgc.addEventListener('mouseenter', function() {
    bgs.forEach(bg => {
      console.log(bg);
    })
  })
})
.cons {
        width: 200px;
        height: 300px;
        background-color: red;
        position: relative;
        display: inline-block;
        left: 30%;
        top: 200px;
    }
    .bgs {
        width: 200px;
        height: 70px;
        background-color: blue;
        position: absolute;
        left: 0;
        bottom: 0;
        cursor: pointer;
        opacity: 0;
    }
    p {
        text-align: center;
        color: #fff;
        padding-top: .6rem;
    }
<div class="cons">
    <div class="bgs">
        <p>hello 1</p>
    </div>
</div>
<div class="cons">
    <div class="bgs">
        <p>hello 2</p>
    </div>
</div>
<div class="cons">
    <div class="bgs">
        <p>hello 3</p>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I suggest you delegate from the nearest static container

change

const tgt = e.target.closest("div.bgs");

to

const tgt = e.target.closest("div.cons");

as necessary

document.getElementById("container").addEventListener('mouseover', function(e) {
  const tgt = e.target.closest("div.bgs");
  if (tgt) console.log(tgt.querySelector("p").textContent);
})
<div id="container">
  <div class="cons">
    <div class="bgs">
      <p>hello 1</p>
    </div>
  </div>
  <div class="cons">
    <div class="bgs">
      <p>hello 2</p>
    </div>
  </div>
  <div class="cons">
    <div class="bgs">
      <p>hello 3</p>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!