javascript - How do I get the value of a cell in a loop using jquery -
i want value of cell related row calculate difference of values in cell want add of same row.
the snippet shows mean wanting data same row on row land on , selected row in instance. other has nothing code.
$(document).ready(function() { $('tbody tr').eq(0).find('td').eq(7).css('background-color', 'green'); $('tbody tr').eq(3).find('td').eq(7).css('background-color', 'green'); $('tbody tr').eq(4).find('td').eq(7).css('background-color', 'green'); });
table, td { border: 1px solid #000; } td { width: 40px; height: 40px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <table> <tbody> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </body>
this jquery code value cells
$(document).on('blur', '.price', function(){ var id = $(this).data("id1"); var price = $(this).text(); edit_data(id, price, "price"); });
i tried fix adding:
$(document).on('blur', '.price', function(){ var id = $(this).data("id1"); var price = $(this).text(); edit_data(id, price, "price"); var id2 = $('.final_price').data("id2"); var final_price = $('.final_price').text(); var diff_total = final_price - price; edit_data(id, diff_total, "diff"); });
the jquery code above returns string of final_price values in every cell have
this code table can see array return multiple cells , each cell has own value
<?php $connect = mysqli_connect("localhost", "root", "", "test_db"); $output = ''; $sql = "select * tbl_sample order id desc"; $result = mysqli_query($connect, $sql); $output .= ' <div class="table-responsive"> <table class="table table-bordered"> <tr> <th width="10%">id</th> <th width="40%">price</th> <th width="40%">final price</th> <th width="10%">delete</th> </tr>'; if(mysqli_num_rows($result) > 0){ $final_price_total = 0; $price_total = 0; while($row = mysqli_fetch_array($result)){ $output .= ' <tr> <td>'.$row["id"].'</td> <td class="price" data-id1="'.$row["id"].'" contenteditable>'.$row["price"].'</td> <td class="final_price" data-id2="'.$row["id"].'" contenteditable>'.$row["final_price"].'</td> <td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td> </tr> '; $final_price_total += $row["final_price"]; $price_total += $row["price"]; } $output .= ' <tr> <td></td> <td class="price_total" id="price_total" name="price_total" value="'.$price_total.'" contenteditable>'.$price_total.'</td> <td class="final_price_total" id="final_price_total" name="final_price_total" value="'.$final_price_total.'" contenteditable>'.$final_price_total.'</td> <td></td> </tr> '; $output .= ' <tr> <td></td> <td id="price" contenteditable></td> <td id="final_price" contenteditable></td> <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td> </tr> '; } else{ $output .= '<tr> <td colspan="4">data not found</td> </tr>'; } $output .= '</table> </div>'; echo $output; ?>
i'm assuming trying read final price of same row i.e. inside parent tr of price. following code work.
$(document).on('blur', '.price', function(){ var $el = $(this); var id = $el.data("id1"); var price = $el.text(); edit_data(id, price, "price"); var $fel = $el.parents('tr:first').find('.final_price'); var id2 = $fel.data("id2"); var final_price = $fel.text(); var diff_total = final_price - price; edit_data(id, diff_total, "diff"); });
Comments
Post a Comment