learningbyexample learningbyexample - 3 months ago 8
Javascript Question

How do I get the value of a cell in a loop using jquery

I want to get the value of a cell related to the row to calculate the difference of the values in another cell that I want to add of the same row.

the snippet shows what I mean by wanting to get the data from the same row on any row that i land on and only that selected row in that instance. Other than that it has nothing to do with the code.



$(document).ready(function() {
$('tbody tr').eq(2).find('td').eq(2).css('background-color', 'green');
$('tbody tr').eq(2).find('td').eq(5).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 is my jquery code to get the value from the cells

$(document).on('blur', '.price', function(){
var id = $(this).data("id1");
var price = $(this).text();
edit_data(id, price, "price");
});


I tried to fix this is by 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 a string of all final_price values in every cell that I have

this is the code for the table but as you can see it is an array the return multiple cells and each cell has its own value

<?php

$connect = mysqli_connect("localhost", "root", "", "test_db");
$output = '';
$sql = "SELECT * FROM tbl_sample ORDER BY 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;
?>

Answer

I'm assuming you are trying to read the final price of the same row i.e. inside parent tr of price. following code will 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");

    });