You are viewing limited content. For full access, please sign in.

Question

Question

Forms Table . Each function no longer working properly. Is my code example wrong?

asked on July 12, 2017 Show version history

I had this code ready to go and formatted, seems it has always worked in the past. Today it is not working properly and I can't see anything wrong with the syntax.

 

$('.ptoTable').each(function () {

             hours = $(this).find('.ptohours input').val();

              alert(hours);
      
        
        
            });

If I have a table with the class ptoTable with 1 row it alerts the value of hours twice. How is it running twice if there is only 1 row. Furthermore, if I have more than one row, it still just alerts twice, and it only alerts the first value of the first row each time.

0 0

Replies

replied on July 12, 2017

Ok just found the problem. Can't call a table by class name only (not sure what this does). You must call the "tableclassname tbody tr"

0 0
replied on July 12, 2017 Show version history

$('.ptoTable').each will not iterate through the contents if the class is assigned to the table, it will iterate through each element that has the matching class name.

To iterate through each field in the table, you would want to use 

$('.ptoTable input').each

This will run the loop on every input element contained within the table.

The reason "tdbody tr" helped is that it loops on each row of the table, but using '.ptoTable input' should make that unnecessary and has the benefit of allowing you to use $(this).val() directly since it would iterate over the fields instead of the rows.

0 0
replied on July 12, 2017 Show version history

Does this work even if all the fields are not of type "input"? Select Select?

 

Also when I say "this" which field does it return?

0 0
replied on July 12, 2017

No, if you need to grab different field types, you would need to add an additional selector for each type. For example, to include inputs and selects, you would use the following:

$('.ptoTable input, .ptoTable select').each
0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.