I have a list of products with prices, before the productname I have a checkbox. What I want is when I checked a checkbox the price must show up in the table.
The products are fetched by a loop from MySQL.
This is my code for a better view:
<?
$query = "SELECT upgrades.upgrade_name, upgrades.upgrade_id, upgrades.volgorde, upgrades.price
FROM upgrades
LEFT JOIN upgrades_to_products
ON upgrades.upgrade_id = upgrades_to_products.upgrade_id
WHERE upgrades_to_products.product_id = 20
AND status = 1
ORDER BY upgrades.volgorde";
$sql = mysql_query("$query");
while ($fill = mysql_fetch_array($sql)) {
$upgrade_id = $fill['upgrade_id'];
$upgrade_name = $fill['upgrade_name'];
$price = $fill['price'];
$name = str_replace(' ', '_', $upgrade_name);
$name = strtolower($name);
echo '
<tr class="'.$class.'">
<td>
<input id="'.$name.'" name="'.$name.'" type="checkbox" value="'.$price.'" />
</td>
<td>
<a href="http://www.informer.nl/modules/?ID='.$_GET['ID'].'&upgrade_id='.$upgrade_id.'" target="_blank">'.$upgrade_name.'</a>
<a href="http://www.informer.nl/modules/?ID='.$_GET['ID'].'&upgrade_id='.$upgrade_id.'" target="_blank">
<img src="images/vraag.jpg" border="0" style="margin-left:5px;" />
</a>
</td>
<td align="right">€ '.sprintf('%01.2f', $price).'</td>
<td align="right"><div class="module_value"></div></td>
</tr>';
}
?>
You can see the checkbox between the first <td></td>
. So when I checked one of the checkboxes that value must show up in the <div class="module_value"></div>
.
I tried something like this:
$("input[type=checkbox]").click(function() {
var module_price = $("input[type=checkbox]").val();
$(".module_value").text(module_price.toFixed(2));
});
but that didnt work so I hope someone can help me out.
Thanks in advance!
You can use :checkbox
selector for accesing checkboxes and .change()
would be a better choice for your job. With .closest()
you can find parent tr
element and then you can find .module_value
from there and change it's text.
$(':checkbox').change(function() {
var $this = $(this),
$tr = $this.closest('tr');
$tr.find('.module_value').text(this.checked ? $this.val() : '');
});
You can see it in action here.