I am new to JavaScript and ajax and I am setting up a cart and I need to get the id of the select tag and the value of the option selected, and pass through ajax. id is dynamically created because data is coming from DB
I have tried this, but issue is this works well for the first row of the data but not for all because of the "id"
<script type="text/javascript">
var subtotal = 0,
adult_price = 0,
kid_price = 0;
$('#adult_quantity').change(function() {
// Remove any previously set values
$('#subtotal, #total_box').empty();
$(this).find('option:selected').each(function() {
// Check that the attribute exist, so that any unset values won't bother
if ($(this).attr('value')) {
adult_price = $(this).data('price');
var span_Text = document.getElementById("adult-price").innerText;
adult_price = adult_price * span_Text;
subtotal = adult_price + kid_price;
$('#subtotal').append( subtotal);
}
});
});
$('#kids_quantity').change(function() {
// Remove any previously set values
$('#subtotal, #total_box').empty();
$(this).find('option:selected').each(function() {
// Check that the attribute exist, so that any unset values won't bother
if ($(this).attr('value')) {
kid_price = $(this).data('price');
var span_Text = document.getElementById("kids-price").innerText;
kid_price = kid_price * span_Text;
subtotal = adult_price + kid_price;
$('#subtotal').append( subtotal);
}
});
});
</script>
I have this code is from cart.php
<tr id="package_row">
<td>
<?php echo "<b><strong>".$row["subpackage_name"].": </strong></b>"; ?>
<?php echo $row["subpackage_detail"]; ?>
</td>
<td>
<b>Adults</b>
<select name="adult_quantity" <?php echo "id=adult_quantity".$increment; ?> onchange="getPrice(this.value)" >
<?php
for($i = 0; $i<51; $i++)
{ ?>
<option data-price=<?php echo $i; ?> value=<?php echo $i; ?>><?php echo $i; ?></option>
<?php } ?>
</select>
<br/>
Price: <span <?php echo "id=adult-price".$increment; ?>><?php echo $row["subpackage_adult_price"]; ?></span>
<b>Kids</b>
<select name="kids_quantity" <?php echo "id=kids_quantity".$increment; ?> onchange="getPrice(this.value)">
<?php
for($i = 0; $i<51; $i++)
{ ?>
<option data-price=<?php echo $i; ?> value="$i"><?php echo $i; ?></option>
<?php } ?>
</select>
<br/>
Price: <span <?php echo "id=kids-price".$increment; ?> ><?php echo $row["subpackage_kids_price"]; ?></span>
</td>
<td><input type="date"/></td>
<td><span <?php echo "id=subtotal".$increment; ?>></td>
<td><a ><span class="icon-remove" id="delete"></span></a></td>
</tr>