Home > Net >  Jquery loop through elements of dom
Jquery loop through elements of dom

Time:12-31

I know this is stubit but how can i writte this code better using JQuery? Really makes me sick when i see it

Actually what this code does is that on a dom tree there are some elements with the same class and I am selecting the second and the forth of these elements.

$('.hc-inline-block').each(function(i){
        if(i==1){
             st_time= $(this).html(); 
            console.log(st_time);
        }
        else if(i==3){
            end_time= $(this).html(); 
    console.log(end_time);
        }
    
  });
    

CodePudding user response:

Consider the following.

var st_time = $('.hc-inline-block').eq(1).html();
var end_time = $('.hc-inline-block').eq(3).html();

See more: https://api.jquery.com/eq/

CodePudding user response:

You can use $('.hc-inline-block:eq(1), .hc-inline-block:eq(3)').each(function() {... to get only the 2nd and 4th element and do something with them.

  • Related