I'm trying to sum the price after selecting an item in the dropdown, but it only displays NaN. The value of dropdown are retrieved from MySQL database
<?php
//database connection here
?>
<table border=" 1">
<thead>
<tr>
<th>Component</th>
<th>Item Name</th>
<th>Price </th>
</tr>
</thead>
<tbody>
<tr>
<td>CPU</td>
<td>
<?php
//Retrieving CPU table
$query = $conn->query("SELECT * FROM cpu");
echo '<select onChange = parseFloat($("#cpuprice").val($(this).find("option:selected").attr("cpuprice")))>';
echo "<option>---select your CPU---</option>";
while ($obj = $query->fetch_object()) {
echo '<option cpuprice = "' . $obj->price . '" >' . $obj->cpuname . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="cpuprice" disabled value="">
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>GPU</td>
<td>
<?php
//Retrieving GPU table
$query = $conn->query("SELECT * FROM gpu");
echo '<select onChange = parseFloat($("#tpuprice").val($(this).find("option:selected").attr("gpuprice")))>';
echo "<option>---select your GPU---</option>";
while ($obj = $query->fetch_object()) {
echo '<option gpuprice = "' . $obj->price . '" >' . $obj->gpuname . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="tpuprice" disabled value="">
</td>
</tr>
</tbody>
</table>
this is the jQuery that I used I don't know if this is right but I search everywhere and NaN is the best result I got so far, sorry I'm just new to using JavaScript or jQuery
<span class="totalprice">Total: </span>
<script>
var total = 0;
$('select').change(function() {
var cpu = ($('#totalprice').val($(this).find('option:selected').attr('cpuprice')))
var gpu = ($('#Totalprice').val($(this).find('option:selected').attr('gpuprice')))
total = parseFloat(cpu) + parseFloat(gpu);
$('.totalprice').text('₱' + total);
})
</script>
