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

288
Views
get index number from parent node using indexOf.call javascript

I have following DOM structure

<ul class="main-products">
  <li class="product">product</li>
  <li class="product">product</li>
  <li class="product">product</li>
<section class="banner">
<ul class="sidebar-products">
  <li class="product">product</li>
  <li class="product" id="element">product</li> // this is 5th product
  <li class="product">product</li>

I'm trying to get the index number(4) of the marked 5th product

var chl = document.querySelector('li#element')
var prt = chl.parentNode;
var idx = Array.prototype.indexOf.call(prt.children, chl);

Above code gives me 1 because they have different parents. How can I modify it to include all products from other parents?

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

0

You just needed to access the prt.childNodes.

var chl = document.querySelector('li#element')
var prt = chl.parentNode;
const index = Array.prototype.indexOf.call(prt.childNodes, chl)
console.log(index)
<ul class="main-products">
  <li class="product">product</li>
  <li class="product">product</li>
  <li class="product">product</li>
<section class="banner">
<ul class="sidebar-products">
  <li class="product">product</li>
  <li class="product" id="element">product</li> // this is 5th product
  <li class="product">product</li>

about 4 years ago · Juan Pablo Isaza Report

0

Get all of the li.products, then find the index by testing the ids...

const products = document.querySelectorAll('li.product');
const i = Array.from(products).findIndex(p => p.id === 'element');
console.log(i);
<ul class="main-products">
  <li class="product">product</li>
  <li class="product">product</li>
  <li class="product">product</li>
<section class="banner">
<ul class="sidebar-products">
  <li class="product">product</li>
  <li class="product" id="element">product</li> // this is 5th product
  <li class="product">product</li>

about 4 years ago · Juan Pablo Isaza Report

0

This is my attempt to get all the product using document.querySelectorAll, it will select all the HTML tag with the class of product. After that change it into an array of ids, then find the index of target id from that array.

// get all the li with class product and turn it into array
const all_products = [...document.querySelectorAll('li.product')]

// convert array of li to array of id
const all_products_id = all_products.map(li => li.id)

// find the id from the array of id
console.log("The index of product with id of element = " + all_products_id.indexOf("element"))
<ul class="main-products">
  <li class="product">product</li>
  <li class="product">product</li>
  <li class="product">product</li>
 </ul>

<section class="banner">
<ul class="sidebar-products">
  <li class="product">product</li>
  <li class="product" id="element">product</li> <!-- this is 5th product -->
  <li class="product">product</li>
</ul>
</section>

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!