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

137
Visualizações
Adding content by searching elements href using Javascript

I've got a list of products which are Men & Women, using Javascript below each product I want to print a message to tell users that these products are available in whatever sizes are in-stock. To determine which products are Men & Women, I'm looking at the href and targeting "womensclothing" & "mensclothing" which works but when i go to print the messages they don't sit below the products correctly as you can see when you run the code. Any help on this would be appreciated.

Please note i do not have access to the html to make changes.

var womensClothing = document.querySelectorAll('.productTitle[href*="womensclothing"]');
var womenSizeLocation = document.querySelectorAll('.productPrice');
womensClothing.forEach(function(link, i) {
  womenSizeLocation[i].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + link.getAttribute('href') + "><p>Women's sizes:xs, s, m, l</p></a>");
});


var mensClothing = document.querySelectorAll('.productTitle[href*="mensclothing"]');
var menSizeLocation = document.querySelectorAll('.productPrice');
mensClothing.forEach(function(links, el) {
  menSizeLocation[el].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + links.getAttribute('href') + "><p>Men's Sizes: s, m, l, xl </p></a>");
});
.productWrapper {
  margin: 0 0 10px;
  border: 1px solid black;
}
<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since the class for the price is same for all products: productPrice, you need to use the CSS Sibling operator ~ to select the elements that is the sibling of the specific <a> element.

var womensClothing = document.querySelectorAll('.productTitle[href*="/womensclothing"]');
var womenSizeLocation = document.querySelectorAll('.productTitle[href*="/womensclothing"] ~ .productPrice');
womensClothing.forEach(function(link, i) {
  womenSizeLocation[i].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + link.getAttribute('href') + "><p>Women's sizes:xs, s, m, l</p></a>");
});


var mensClothing = document.querySelectorAll('.productTitle[href*="/mensclothing"]');
var menSizeLocation = document.querySelectorAll('.productTitle[href*="/mensclothing"] ~ .productPrice');
mensClothing.forEach(function(links, el) {
  menSizeLocation[el].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + links.getAttribute('href') + "><p>Men's Sizes: s, m, l, xl </p></a>");
});
.productWrapper {
  margin: 0 0 10px;
  border: 1px solid black;
}
<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

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