How to select an element with class and loop through each one in JavaScript?
author
,
A guide

How to select an element with a class in javascript and loop through each element like in jQuery.

  • Total 1 Answer
  • 42
Can You answer this question?
author
,
A guide

We can select any element of webpage using document.querySelector  and loop through each element having same class with forEach  function. Follow below example to understand it better:


    flag = 0;
    idtochoose = '';
    document.querySelector('.ty-shipping-options__method input').forEach(function(node) {
        console.log(node.id);

        if(node.checked){
            flag = 1;
        }
        if(idtochoose == ''){
            idtochoose = node.id;
        }
    });

    if(flag == 0){
        setTimeout(function(){
            document.getElementById(idtochoose).click();
        },500);
    }