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

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -