javascript - jQuery Each Incremented Counter vs. Selectors -


i finished aspect of project required looping through rows of html table , depending on class of td, text. approached issue 2 different methods , i'm wondering considered best coding practice. also, there advantage using 1 method on other?

method 1:

$('#table tr').each(function(){     var = $(this).find('[class*=someclass]').html();     //do 'something' }); 

method 2:

var x = 0; $('#table tr').each(function(){     var = $(this).find('.someclass' + x).html();     //do 'something'     x++; }); 

this may more opinion-based, personally, this:

$('#table tr').each(function(i){     var = $(this).find('.someclass' + i).html();     //do 'something' }); 

the index of each element passed argument.

edit:

to expand on karl-andrégagnon said, 2 '.someclass' selectors behave differently. in first example, select elements 'someclass', regardless of numerical suffix. second example select classes specified numerical suffix.

if doing same thing each '.someclass' element, regardless of number on class, may not need "each" @ all, , start selector such as:

'#table tr [class*=someclass]' 

Comments

Popular posts from this blog

javascript - ScrollTo Effect (href to div) -

javascript - ng-class not responding to change in model in Angular? -

javascript - document.registerElement extending HTMLInputElement prototype while using custom tag name to avoid implicit browser style -