I want that if a button is clicked, 0.99 will be added to my var amount. I cant get this done and i dont know why. Anyone an idea?
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount = 0.99++;
}
});
console.log(amount);
Use var amount += 0.99; instead of using var amount = 0.99++;
Try,
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += parseFloat(0.99);
}
});
console.log(amount.toFixed(2));
Try this:
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += parseFloat(0.99);
}
});
console.log(amount);
parseFloat
Change to
amount += 0.99;
The problem with your code is that your doing a post increment which means you will increment after the assignment. This means you won't see the effects of the increment in the amount variable.
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
amount += 0.99;
}
});
console.log(amount);
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += 0.99;
}
});
console.log(amount);
Try this,
<script type="text/javascript">
var amount = 0;
$(function(){
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
amount = amount + 0.99;
}
console.log(amount);
});
console.log(amount);
});
</script>
var amount = 0.99++;
doesn't work.
What ++ does is increment a var by 1.
In your code 0.99 isn't a variable, therefore it can't increment it. And even if it could, amount would always be equal to 1.99 (0.99 + 1 = 1.99).
So to fix it, you need to increment a variable. Except you're not incrementing by 1, you're incrementing by 0.99, so don't use ++ here.
Just use:
amount += 0.99
That means amount = amount + 0.99
Related
i have a js code that calculate the total amount when being click by checkbox. The user can either choose to select all or select only certain amount that they want to calculate. The function if the user 'select all'is work fine but when they click only certain amount that they want to calculate, if the amount is have ','in it, it will only take the number before the ','for example if the amount is 20,230, it will total the amount as 20.00. it work real fine if the number is only 20230 without the ','. help me please.
function checkedAll () {
var row_counter =0;
$("input[id ^= 'check_'] ").each(function() {
row_counter++;
});//en
var total=0;
for(var i=1; i<=row_counter; i++) {
//alert(i);
if (document.getElementById("checkall").checked==true){
document.getElementById('check_'+i).checked = true;
var amaun = document.getElementById('amaun_'+i).value;
//alert(amaun);
total = parseFloat(total)+parseFloat(amaun);
}
else{
document.getElementById('check_'+i).checked = false;
var amaun = 0;
total = parseFloat(total)+parseFloat(amaun);
}
}
document.getElementById("jum_baucar").value=total.toFixed(2);
}
function calcTotal(){
//alert("here");
var row_counter =0;
$("input[id ^= 'check_'] ").each(function() {
row_counter++;
});//en
var total=0;
for(var i=1; i<=row_counter; i++) {
//alert(i);
if (document.getElementById('check_'+i).checked == true){
var amaun = document.getElementById('amaun_'+i).value;
//alert(amaun);
total = parseFloat(total)+parseFloat(amaun);
}
else{
var amaun = 0;
total = parseFloat(total)+parseFloat(amaun);
}
}
document.getElementById("jum_baucar").value=total.toFixed(2);
}
Those are my js code.
what should I add into it?
Having a , in a number that is applied with parseFloat() treats it as a decimal. For example parseFloat('10,10') => 10. One simple solution is simply removing the , from the string:
amaun = amaun.replace(',',''); // Replace a comma with nothing
Or for several commas using a regexp to act as a "replaceAll()":
amaun = amaun.replace(RegExp(',','g'),'');
Then you can apply parseFloat(amaun):
total = parseFloat(total)+parseFloat(amaun);
I'm making a simple game in HTML/Javascript where everytime a button or hyperlink gets pressed it adds 1 to a counter
<script type="text/javascript">
var count = 0;
function countClicks() {
count = count + 1;
document.getElementById("p2").innerHTML = count;
}
</script>
My question is is there a way to also add a tick (Ascii #10004) equal to the number in the counter. I'm sure this is an easy solve but I've never really used Javascript and this seems the easiest language to do this all in. I appreciate any help given
You can use the HTML decimal: ☑
Just replace the code:
document.getElementById("p2").innerHTML = count;
with the following code:
document.getElementById("p2").innerHTML = "☑ "+count;
Or you can use:
document.getElementById("p2").innerHTML = "✔ "+count;
The result will be like this:
✔ 5
here 5 is your count.
Yes you can. You even don't need a loop to concatenate ticks.
see jsfiddle demo
var count = 5; // count is already 5 for demo purpose
function countClicks() {
count = count + 1;
var ticks = Array(count+1).join("✔");
document.getElementById("p2").innerHTML = count+' '+ticks;
}
countClicks(); # 6 ✔✔✔✔✔✔
Yes, you can use the String.fromCharCode to convert ascii code to character.
Example to display continuos ticks equal to number of count:
<script type="text/javascript">
var count = 0;
function countClicks() {
count = count + 1;
var res = String.fromCharCode(10004);
var output = "";
for (var i = 0; i < count; i++) {
output += res+" ";
}
document.getElementById("p2").innerHTML = output;
}
</script>
This will do :
http://jsfiddle.net/oL83m567/1/
var count = 0;
function countClicks() {
count = count + 1;
var tick='';
for(var i=0;i<count;i++)
tick+= '✔';
document.getElementById("p2").innerHTML = count + tick;
}
It sounds like you want to ADD another tick every time the thing is pressed? I would do it like this:
var count = 0;
function countClicks() {
count = count + 1;
document.getElementById("p2").innerHTML = document.getElementById("p2").innerHTML + '✔';
}
Or, slightly more efficiently:
var p2, count = 0;
function countClicks() {
count = count + 1;
p2 = document.getElementById("p2");
p2.innerHTML = p2.innerHTML + '✔';
}
In that case, count becomes unnecessary, unless you need it for something else.
I'd go for doing it with an array. Advantage of this is that you have the ticks stored as elements in the array and can manipulate them later e.g. change a chosen tick to a cross or have a button which removes a tick. Something like:
<script type="text/javascript">
var ticks=new Array();
function countClicks() {
ticks[ticks.length]="✔";
document.getElementById("p2").innerHTML = ticks.join("");
}
</script>
I need to enter a set of numbers in a textbox in this form 12, 13, 54, 12 and calculate the average of the numbers from the textbox.
Is their any form or something with javascript or jquery to calculate this on realtime or without post back.
Thanks
$(document).ready(function(){
$("#yourtextbox").keyup(function(){
var text = $(this).val();
var arr = text.split(',');
var sum = 0;
var count = 0;
$.each(arr,function(index,value){
var currentItem = parseInt(value);
if (!isNaN(currentItem)){
sum+= currentItem;
count++;
}
});
console.log(sum/count);
$("#result").text(sum/count);
});
});
Demo
$('.x').keyup(function(){
var total = 0;
$('.x').each(function()
{
var value = parseInt($(this).val());
if(!isNaN(value))
total += value;
})
alert(total);})
I saved a fiddle http://jsfiddle.net/hcs49/; instead of using class selector, you could also use input[type=text]
How to add values from all input with name name="TotalInline[]"?
The following does not seams to work:
var total = 0;
$.each('input[name="TotalInline[]"];,function() {
total += this;
});
This should work :
var total = 0;
$('input[name="TotalInline"]').each(function() {
// assuming you have ints in your inputs, use parseFloat if those are floats
total += parseInt(this.value, 10);
});
var total = 0;
$.each($('input[name="TotalInline[]"]'), function() {
total += parseInt(this.value, 10);
});
You have some serious syntax errors, try this:
var total = 0;
$('input[name="TotalInline[]"]').each(function () {
total += parseInt(this.value, 10);
});
Try like this...
var total = 0;
$('input[name="TotalInline[]"]').each(function() {
total += parseInt($(this).val(),10);
});
var total = 0;
$('input[name="TotalInline[]"]').each(function() {
total += +this.value.replace(/[^\d.]/g, '');
});
Uses a quick regex to filter out only the numbers (and decimal point).
Uses a + prefix to convert to a number.
I have two form inputs which display the calculated invoice sub-total and total of a form.
The problem I'm having is it's not displaying 2 decimal places.
Subtotal function:
function calcProdSubTotal() {
var prodSubTotal = 0;
$(".row-total-input").each(function(){
var valString = $(this).val() || 0;
prodSubTotal += parseInt(valString);
});
$("#product-subtotal").val(prodSubTotal);
};
Total function
function calcOrderTotal() {
var orderTotal = 0;
var productSubtotal = $("#product-subtotal").val() || 0;
var productTax = $("#product-tax").val() || 0;
var orderTotal = parseInt(productSubtotal) + parseInt(productTax);
var orderTotalNice = "$" + orderTotal;
$("#order-total").val(orderTotalNice);
};
How do I go about displaying two decimal places?
change $("#product-subtotal").val(prodSubTotal);
to $("#product-subtotal").val(addDecimals(prodSubTotal));
and change $("#product-subtotal").val(prodSubTotal);
to $("#product-subtotal").val(addDecimals(prodSubTotal));
function addDecimals(a){
a += "";
var i=a.indexOf('.');
if(i<0){
return a + ".00";
}
var j = a.substring(i);
console.log(j);
if(j.length<3){
for(var k=j.length;k<3;k++)
a+='0';
return a;
}
if(j.length>3){
return a.substring(0, i)+j.substring(0,3);
}
}
You may want to look here:
http://www.mredkj.com/javascript/nfbasic2.html
Basically you can use toFixed(2), but then you get some rounding.
Or, if rounding is bad you can do parseInt(productTax * 100) / 100.
If you are working with real numbers it would be better to use parseFloat instead of parseInt. To format the number you could use the toFixed function:
$("#product-subtotal").val(prodSubTotal.toFixed(2));