I can not find an error in my code. The code should add a different id to all table cells. And the same is added to all of them, what's the problem?

$('.price-table td').each(function(){ var i = 0; i < 1000; i++; $(this).attr("id",+i); }); 

    2 answers 2

    Note the callback function parameters.

    https://api.jquery.com/each/

     $('.price-table td').each(function(index, element) { $(element).attr("id", "id_" + index); }); 

    You can without each :

     $('.price-table td').attr("id", function(index, oldAttr) { return "id_" + index; }); 

    Regarding your code:

    Try to explain to yourself what each line / function / operator does in it.

    • possible without each. - Grundy
      $('.price-table td').each(function(key, value){ $(this).attr('id', key); });