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

292
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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