user1296114 - 2 months ago 20
Javascript Question

# Calculations with javascript

I found this script and its kind of what I need to learn right now.

Similar code

I want to get some data from PHP and with that date to do the calculation!

But obviously i dont understand how to do it yet!

``````\$(window).load(function(){
\$('div#cont-sum-fields').on('change', 'input', function() {
//        var total =  (parseInt(\$( "#cont-sum-1" ).val()) +  parseInt(\$( "#cont-sum-2" ).val())) * parseInt(\$( "#cont-sum-3" ).val()) ;
var printsmall = 6;
//var pricesmall = <? echo \$priceAUD[4][2];?>; // output 6.0
var total =  (parseInt(\$( "#cont-sum-1" ).val()) +  parseInt(\$( "#cont-sum-2" ).val())) * pricesmall) ;
\$('#cont-sum-fields').find('#total-cont-sum').val(total);
find('pricesmall').val(pricesmall);
});
}); ``````

``````<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='cont-sum-fields'>
(<input type="number" id="cont-sum-1" /> +
<input type="number" id="cont-sum-2" />) x
<input id="pricesmall" type="number" disabled /> =
<input id="total-cont-sum" type="number" disabled />
</div>  ``````

So a little summary of what I want to get done:

First number + Second number * php or the var printsmall = total!

Here is your code modified in the way I think you want it.

``````\$(window).load(
function(){
var printsmall = 6;
\$('#pricesmall').val(printsmall);
\$('#cont-sum-fields').on('change', 'input',
function() {
var
s1 = \$('#cont-sum-1').val(),
s2 = \$('#cont-sum-2').val();
if (s1 && s2) {
\$('#total-cont-sum').val(
(parseFloat(s1) + parseFloat(s2)) * printsmall);
}
}
)
}
);``````
``````<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='cont-sum-fields'>
(<input type="number" id="cont-sum-1" /> +
<input type="number" id="cont-sum-2" />) x
<input id="pricesmall" type="number" disabled /> =
<input id="total-cont-sum" type="number" disabled />
</div>``````