Javascript Multiplication and Totals - javascript

I have several fields that people will put in loan totals... We'll say loan1 and loan2 just to keep it simple.
window.addEvent('domready', function() {
$('loan1').addEvent('change', rekenen1);
$('loan2').addEvent('change', rekenen1);
});
function rekenen1(){
$('subtotal').value = Number($('loan1').value) + Number($('loan2').value) ;
}
When they enter the data I have it create a subtotal of all the loans. Now I want to make a grand total by taking 1% of the subtotal and adding 295 for the grand total. I need help making this into javascript.
So the equation:
loan1 + loan2 = subtotal
(subtotal*.01)+295=Grandtotal
Thank you for your help!

Provided you have a field with id grandtotal you can just add a line to you rekenen1 function:
function rekenen1(){
var subtotal = Number($('#loan1').val()) + Number($('#loan2').val());
$('#subtotal').val(subtotal);
$('#grandtotal').val(subtotal*0.01+295);
}
Edit:
I gone without saying that the addEvent have to change too:
$().ready(function() {
$('#loan1').addEvent('change', rekenen1);
$('#loan2').addEvent('change', rekenen1);
});
Changed rekenen1 function to use the jquery val function too
changed the event

Related

search for a control in content page from content page via a variable passed to the executing function

I have researched much about this but still finds no solution when the calling function passes 3 different parameters that posses the id of 3 control namely txtquan1,txtatthe1,lblamnt1 on the onblur of both the textbox controls for the first row(this repeats for N number of rows) then,how can the javascript executing function can search for the control which is in content page and the calling function is also in the content page. i have used this code but qty have no value at all.....plz assist me..
function funAmount(suffix, quantity, amount, totalamt) {
alert(suffix);
var qty = document.getElementById(suffix + quantity).value;
alert(qty);
var amt = document.getElementById(suffix + amount).value;
var total = parseFloat(qty) * parseFloat(isNaN(amt) ? 0 : amt);
document.getElementById(suffix + totalamt).innerText = isNaN(total) ? 0 : total;
}
<asp:TextBox ID="txtquan1" runat="server" onblur="funAmount(this.id.replace('txtatthe1',''),'txtquan1','txtatthe1','lblamnt1')"></asp:TextBox>
and also used this code but gives the error
var no = document.getElementById('<%=this.Page.FindControl("ContentPlaceHolder1").FindControl(amount).ClientID %>').value;

How to get GrandTotal without shipping and tax in Magento

I need to put the value of "Grand Total without shipping and tax" into a JavaScript on the success.phtml-page. To add the Grand Total i use the following code:
<?php echo $this->__('%s', $this->escapeHtml($this->getGrandTotal())) ?>
I was thinking about using getSubtotal instead, but that will not be correct in case of any Shopping Cart Price Rule being used. So I think the approach must be something like: "GrandTotal minus shipping minus tax"
But how can I add these parameters to the code above?
EDIT: About using getSubtotal... I would then need to know how to subtract any discount given by coupon, anyone know how to do this? I was thinking about something like this:
<?php echo $this->__('%s', Mage::getModel('checkout/cart')->getQuote()->getSubtotal() - getDiscount()); ?>
...however "getDiscount()" should be something more correct..
Thanks
-Espen
You can try below code in javascript
try {
var total_ship_tax = parseFloat(parseFloat(cart.shipping_amount) + parseFloat(cart.tax_amount)).toFixed(2);
}
catch(er) {
var total_ship_tax = 0;
}
var total_amount = parseFloat(parseFloat(cart.total_amount)+total_ship_tax).toFixed(2);

Count form inputs

I have this page (things which I need are at the bottom) and I need to count values from input (type numbers; it's quantity of product) and print value for each product (there are 4 products) and then all values together. Now I'm trying code for count value for each product. I have this Fiddle (code at the end of this post) code, but at my web it doesn't work. And question is why? And next question, after I will have values for each product, how can I pick them from and adding together?
HTML
<input type="number" value="10" class="number rowModre" min="0" name="pocetModre" id="pocetModre">
<div class="cena" id="cenaModre"></div>
JS
var string = $('#pocetModre').val();
$('#cenaModre').html(string);
Thank you very much.
Your question is not quite clear but i guess you are looking for something like this:
http://jsfiddle.net/akpn3/522/
var total = 0;
$(".number").each(function () {
$(".listprice").append("US $" + $(this).val() +"</br>");
total = total + parseInt($(this).val());
});
$(".listprice").append("Total : US $" + total);

Jquery to total up lines on dynamically generated tables

I have some code that appends new table rows when data is inserted into an existing row.
You can see the code in action here.
On entering a product, the new row is appended to the table with a unique input name.
The user is then required to enter a price and qty.
What I want is a 2 step calculation. In the input box Total, I want the sum of price * qty for that specific row. Secondly, I want the div grandtotal to display the sum of all the input boxes, all dynamic to the number of rows the user inserts.
Normally I would just use:
function fill() {
var txt8 = document.getElementById("TextBox8").value;
var txt9 = document.getElementById("TextBox9").value;
document.getElementById("TextBox10").value = txt8 + txt9;
}
But this is for static a static table where names and ID's are known. How do I make this work for a dynamically generated table?
Apologies, I forgot to give you the fiddle link: http://jsfiddle.net/yUfhL/230/
To get all inputs that have an ID that starts with linetotal, you could write the following in jQuery:
$('input[id^=linetotal]')
Now, to calculate the total, just iterate over the collection:
var total = 0;
$('input[id^=linetotal]').each(function() {
total += parseInt(this.value, 10) || 0;
});
Your output, then:
$('#grandtotal').text(total);
Edit
To first calculate the line totals for every line, you could iterate through them like so:
$('table.order-list tr').each(function() {
var price = parseInt( $('input[name=price]', this).val(), 10 );
var qty = parseInt( $('input[name=qty]' , this).val(), 10 );
$('input[id^=linetotal]', this).val(price * qty);
});
Of course, you wouldn't have to iterate over all rows at all times. At the change event, you're only interested in the current row, which is accessible by $(this).closest('tr')

Calculation using jquery onkeypress

I have this type of exercise in jquery. Go here http://jsfiddle.net/LAntL/3/
For Item 1, if I add #no of quantity of Size 7, it should be multiplied by Item 1's Price displayed in text box and the result should be shown in Item 1's Ext textbox.
then if I do same for size 8.5 of Item 1, it should perform the same.
But the Ext will have (Price x Size 7 Qty) + (Price x Size 8.5 Qty)
Same will happen for Item 2. And along with this, Total quantity and Total price will be updated on keypress event of each textbox.
I'm a beginner in jquery and got this killing exercise.
Please help someone.
Thanks in advance.
I'd suggest that you assign some classes to your fields so that jQuery can easily find them:
"lineQty" - add this class to all the qty fields
"lineTotal" - add this class to all the line total fields
"linePrice" - I think you can see where I'm going with this
"lineExt" - add to all ext fields
Then the following .keyup function will do the updates on all lines automatically.
$(document).ready(function() {
$(".lineQty").keyup(function() {
// 'this' is the field the user is typing in
// so find the tr that it belongs to
var $row = $(this).closest("tr"),
total = 0,
// get the price for the current row:
price = $row.find(".linePrice").val();
// loop through every qty field for the current row and add
// the entered value to the total, using unary plus to convert
// to a number, and a default of 0 if non-numeric data is entered
$row.find(".lineQty").each(function() {
total += +this.value || 0;
});
// set the total for the current row
$row.find(".lineTotal").val(total);
// set the price for the current row
$row.find(".lineExt").val(total * price);
// now add up the grand totals
var grandTotal = 0,
grandPrice = 0;
$(".lineTotal").each(function() {
grandTotal += +this.value || 0;
});
$(".lineExt").each(function() {
grandPrice += +this.value || 0;
});
$("[name='data[totalQuantity]']").val(grandTotal);
$("[name='data[totalPrice]']").val(grandPrice);
});
});
Working demo: http://jsfiddle.net/LAntL/5/ (only works on the first line because I couldn't be bothered assigning classes to all your fields - you would need to add the classes I talk about above to every field as per what I did for the first line).
Okay, you want to learn something, so I will describe a possible way, and don't give you the finished solution.
First of all, you should mark the tables which contains items with class="item". Now you can write a selector in jQuery which gets every item separately. Then you should mark all cells (<td>) which have a special function. Total, Price, Disc and Ext. Your table should look like this:
<table class="item">
<tr>
<td>...</td>
<td class="total">...</td>
<td class="price">...</td>
<td class="disc">...</td>
<td class="ext">...</td>
</tr>
</table>
Now you can write a selector for all input fields inside of the table with class "item".
$('.item input').change(function(){});
Inside of this function you can handle the calculation. At first I would recommend to get the item:
$('.item input').change(function(){
var item = $(this).closest('table.item');
var totalInput = $(item).find('.total input');
var priceInput = $(item).price('.price input');
// ...
var allWriteableInputs = $(item).find('input[readonly!="readonly"]');
// do the calculation
var sum = 0;
$(allWriteableInputs).each(function(i, input){
sum += $(input).val();
});
$(totalInput).val(sum);
});
If you go this way, you don't need:
The onkeypress="return isNumberKey(event);" and
the id="product1Total" for every inputbox
I hope this thought-provoking impulse helps you a little bit. You have still a long way to go, my young padawan ;-)

Categories

Resources