JSFiddle - React, Tailwind, and code Playground
HTML
<form id="calc" name="calc">
<p>
<H1>Quote Builder</H1>
</p>
<p>Please input the number of songs you wish to submit for mixing. </p>
<p>
<input id="mixes" name="mixes" type="text" />
<label for="mixes">Number of mixes</label>
</p>
<p>Optional Extras (ÂŁ25 each):</p>
<p>
<input type="checkbox" name="extras_stems" value="25" class="chk-option" /> <label for="extras_stems">24bit Stems (ready for mastering)</label>
<select data-role="extras_stems" name="extras_stems_count" class="hiddenSelect">
</select>
</p>
<p>
<input type="checkbox" name="extras_acapella" value="25" class="chk-option" /> <label for="extras_acapella">A Capella Mix</label>
<select data-role="extras_acapella" name="extras_acapella_count" class="hiddenSelect">
</select>
</p>
<p>
<input type="checkbox" name="extras_instr" value="25" class="chk-option" /> <label for="extras_instr">Instrumental Mix</label>
<select data-role="extras_instr" name="extras_instr_count" class="hiddenSelect">
</select>
</p>
<p>
<input name="calculateTotal" id="calculateTotal" type="button" value="Calculate Total" />
</p>
<div id="result" style="clear:both;">
<strong>Total:</strong>
<span id="total"></span>
<strong>Per mix:</strong>
<span id="permix"></span>
</div>
</form>
CSS
/* hide all the selects when the page loads */
.hiddenSelect {
/* Styles */
display:none;
}
JavaScript
$(document).ready(function() {
$('#calculateTotal').click(function() {
//individual mix price before reduction
var mixPriceIndiv = 50;
//number of mixes selected by user
var nummixes = parseInt($('#mixes').val());
//get mix price based on number of mixes
var mixPrice = getmixPrice(nummixes);
//for each of the chk-option checkboxes, add values if checked
$('.chk-option').each(function() {
if( $(this).is(':checked') ) {
if( nummixes > 1) {
//get value of select
selVal = $('select[data-role="'+$(this).attr("name")+'"]').val();
mixPrice+= parseInt($(this).val()) * parseInt(selVal);
}else {
mixPrice+= parseInt($(this).val());
}
}
});
//grand total
$('#total').html('£' + mixPrice.toFixed(2));
//per mix
$('#permix').html('£' + mixPriceIndiv.toFixed(2));
});
//generate selects based upon the number of mixes added
$('#mixes').on('keyup', function() {
var thisVal = parseInt($(this).val());
if(thisVal > 1) {
generateSelect(thisVal);
}
});
//show the corresponding select when the checkbox is clicked...
$('.chk-option').click(function() {
//hide the select if its already visible...
$('select[data-role="'+$(this).attr("name")+'"]').toggle();
});
function getmixPrice(el) {
numS = parseInt(el);
//calculate mix price
shPrice = 50*numS;
//get percentage value based on number of mixes e.g. if 2 mixes, take off 1%
percentVal = (el!==1) ? ((numS-1)/100)*shPrice : 1 ...