D
D
devilwish2020-05-05 21:57:20
JavaScript
devilwish, 2020-05-05 21:57:20

How to access all elements with a certain selector in jquery?

<script>

$(document).ready(function(){
document.querySelector('.increase').onclick = inCrease;
document.querySelector('.decrease').onclick = deCrease;
function inCrease(){
  let products = document.querySelector('.sorting');
  console.log(products);
  for (let i = 0; i < products.children.length; i++){
    for (let j = i; j < products.children.length; j++){
      if (+products.children[i].getAttribute('data-sort') > +products.children[j].getAttribute('data-sort')){
         replacedNode = products.replaceChild(products.children[j], products.children[i]);
         insertAfter(replacedNode, products.children[i]);
      }

    }
  }
}
function deCrease(){
  let products = document.querySelector('.sorting');
  console.log(products);
  for (let i = 0; i < products.children.length; i++){
    for (let j = i; j < products.children.length; j++){
      if (+products.children[i].getAttribute('data-sort') < +products.children[j].getAttribute('data-sort')){
         replacedNode = products.replaceChild(products.children[j], products.children[i]);
         insertAfter(replacedNode, products.children[i]);
      }

    }
  }
}

function insertAfter(elem, refElem){
  return refElem.parentNode.insertBefore(elem, refElem.nextSibiling);
}
});
            </script>

I wrote this sorting by price, how to access all elements with the "sorting" selector? Now it works only in the first

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wyzemind, 2020-05-05
@wyzemind

querySelector(".sorting") only works on the first element, querySelectorAll(".sorting") works on all.
Everything is elementary in jquery - $(".sorting")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question