jQuery gets <td> text from <tr> id, <td> is generated dynamically, so I don't know how this is possible

I already have a jQuery function to complete the task I need, but is there a way to cycle through the <td> cells of a specific <tr> using id = "Generated_rows"

 <table> <tr id="generated_rows"> <td class="row_class" id="row_id_1">text 1</td> <td class="row_class" id="row_id_2">text 2</td> <td class="row_class" id="row_id_3">text 3</td> <td class="row_class" id="row_id_4">text 4</td> <td class="row_class" id="row_id_5">text 5</td> </tr> </table> 

Need this:

 <table> <tr id="generated_rows"> <td class="row_class" id="row_id_1">text 1.00</td> <td class="row_class" id="row_id_2">text 2.00</td> <td class="row_class" id="row_id_3">text 3.00</td> <td class="row_class" id="row_id_4">text 4.00</td> <td class="row_class" id="row_id_5">text 5.00</td> </tr> </table> 

THE FUNCTION BELOW NOW WORKS!

 // Check for whole numbers and append .00 $('#generated_rows td.row_class').each(function() { var x = Number($(this).text()).toFixed(2); $(this).text(x); }); 
+6
jquery html selector
source share
1 answer

You are close, you just need to use td instead of tr in your selector. Here, my version will add ".00" to the end of the cell text (provided that all numbers, of course, are no longer in a fixed format)

 $("#generated_rows > td.row_class").each(function() { var $this = $(this); var splitText = $this.text().split(' '); splitText[1] = Number(splitText[1]).toFixed(2); $this.text(splitText.join(' ')); }); 
+16
source share

All Articles