Doubt about each() Function

Asked

Viewed 163 times

1

In the example below, is displayed the multiplication of the value of the "checkbox" (if checked) by the value chosen in the "select" of the same line, there is a "reset" of the respective "select" when unchecking the "checkbox" and there is a result in parallel only the sum of the values of the select.

This code is the result of previous questions (lessons) here from Stack, with the tips I managed to make it work perfectly.

My beginner question is, is there redundancy in the code in this way that I did and is it possible to do everything with a single each()? As you can see, there are two "each()":

$('tr').each(function(){ _código_ }); and $('table input').each(function(){ _código_ });.

Following functional example for better understanding: https://jsfiddle.net/8uk440do/18/

From now on, thank you for helping me learn.

  • Just warning, we missed one ) ai in the question code.

  • Thanks @Diegof, I made the corrections.

  • Danilo, one parenthesis is still missing: $('tr').each(function()) and $('table input').each(function(i))

  • Thanks @Diegof, I made the corrections...

  • Danilo, the each() serves to interact a collection running a particular code snippet, in your example, both collections are different and the executed code is also different... so the code does not have redundancy... you could even do it with a single each(), but the code would be dirty and make it difficult to understand it.

  • Got it @Tobymosque, thank you. In your opinion, didactically speaking, this is the best way to do this?

  • Yes, I believe it is.

  • @Tobymosque, thank you

  • @Tobymosque, good morning. Basically what changes for the use of each() methodology in a structure of "Divs", without table structure, tr... ? I am here trying to apply the last learning that you have passed me

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.