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

212
Views
How to get the last child node inside matching selector in JavaScript?
const slidesLength = slideRight.querySelectorAll('div').length / 2;

<div class="right-slide">
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic1.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic2.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic3.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic4.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic5.jpg);"></div>
            </div>
        </div>

I am trying to target the inner most using querySelectorAll(), but I am unsure of how to do that. With the JS code I have right now, I am able to to get the exact number of divs I want, but only by using a different method. I was wondering whether or not there is a way where I can disregard the divs with background-color and only obtain the ones with background image.

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

0

Yes, document.querySelectorAll('.right-slide div[style*=background-image]') will get a list together of all divs inside of .right-slide that have a background-image as an inline style

const bgImageDivs = document.querySelectorAll('.right-slide div[style*=background-image]');
console.log(bgImageDivs)
<div class="right-slide">
  <div style="background-color: #773f47bd">
    <div style="background-image: url(/pic1.jpg);"></div>
  </div>
  <div style="background-color: #773f47bd">
    <div style="background-image: url(/pic2.jpg);"></div>
  </div>
  <div style="background-color: #773f47bd">
    <div style="background-image: url(/pic3.jpg);"></div>
  </div>
  <div style="background-color: #773f47bd">
    <div style="background-image: url(/pic4.jpg);"></div>
  </div>
  <div style="background-color: #773f47bd">
    <div style="background-image: url(/pic5.jpg);"></div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You could use a for loop to check it if the div has background-image or not.

let div = document.querySelectorAll('div');
let num =0;
for(let i =0;i<div.length;i++){
if(div[i].style.backgroundImage !=='')
num++
}
console.log(num)
<div class="right-slide">

            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic1.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic2.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic3.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic4.jpg);"></div>
            </div>
            <div style="background-color: #773f47bd">
                <div style="background-image: url(/pic5.jpg);"></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!