I am subtracting multiple textboxes using JavaScript. I'm adding the total amount in the textboxes.When amount is first entered it works fine but when I change the amount and save it again there is a bug.The value is pulled from database the second time and is in "00.00" format.
The problem is when the value is a 4 digit value say "6500.00" it changes to "6.00". It shows as 6500.00 in the webpage but during calculation in JavaScript it calculates as "6.00".it works fine for 3 digit value like "400.50".Can somebody tell me what's the problem here.
JavaScript:
function fill_balance()
{
var total = document.getElementById("total").value;
var payment = document.getElementById("payment").value;
document.getElementById("balance").value = parseFloat(total) - parseFloat(payment);
}
P.S: I use number_format() function to change the format to 00.00
Related
Doing a form in LiveCycle and using FormCalc. As a part of the form I have 2 check boxes (cbPhoto) & (cbCert), and 2 numeric fields (numPages) and (numResults).
What I would like to achieve is when one or both of the check boxes are selected and an number is entered into (numPages) then do the following calculations and display the results in (numResults).
Calculations:
If (cbPhoto) is selected and say for example 5 is entered into (numPages) I want the calculation to work out $20 for the first page and $1 for every page thereafter (so in this case the results would be $24).
If (cbCert) is selected and say for example 5 is entered into (numPages) I want the calculation to work out $22 for the first page and $2 for every page thereafter (so in this case the results would be $30).
If both check boxes are selected and say for example 5 is entered into (numPages) I want both of the above calculations to run (the results being $54).
I want the (numResults) to remain blank until after a number is placed in (numPages) regardless if the check boxes are selected or not.
I have had very limited success with the following code and would be happy to use JavaScript if it will make it work.
A9.#subform[0].A4::calculate - (FormCalc, client)
if (cbP3==1) and (cbC3==1) then
P1-1+20
and
(P1-1)*2+22
else ""
endif
Any and all help would be greatly appreciated. Cheers
Here is my JavaScript solution. The "calculate" event of numResults will execute whenever any of the other 3 fields change. A non-blank result will be displayed when numPages has a value and at least one of the checkboxes are checked.
form1.#subform[0].cbCert::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].cbPhoto::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].numPages::change - (JavaScript, client)
numResults.execCalculate();
form1.#subform[0].numResults::calculate - (JavaScript, client)
var result = null;
if (numPages.rawValue > 0){
result = 0;
if (cbPhoto.rawValue == 1){
result = numPages.rawValue - 1 + 20;
}
if (cbCert.rawValue == 1){
result += (numPages.rawValue - 1) * 2 + 22;
}
if (result == 0) {
result = null; //show blank instead of 0
}
}
//this line sets the value of the results field:
result;
I'm making my first steps in js and jquery and I'm trying to make simple calculation form,
which
takes numeric variable form form in html (sum)
multiplies it by constat multiplier
multiplies result by a number choosen from dropdown list (total)
and does that on the fly, so to say updates result whenever any variable changes.
code below works, but total result does not update when sum updates. what am I missing here?
$('.pow').keyup(function () {
var sum = 0;
var multip = 4;
sum1 = sum;
$('.pow').each(function() {
sum += Number($(this).val())*parseInt(multip);
sum1 = sum;
});
$("#sum").html(sum.toFixed(2)); });
$('.per').click(function () {
var total = 0;
var period = $("#period").val();
$(".per").each(function() {
total = parseInt(sum1)*parseInt(period);
}); $("#sum1").html(total.toFixed(2)); });
working fiddle here
You calculate totals on ( $('.per').click(.....);, so when you type a number above, nothing happens, because the code does not run)
The easier way to do this would be to automate a click after typing a number.
Add this $('.okr').click(); after this line here $("#sum").html(sum.toFixed(2));
Like so:
$("#sum").html(sum.toFixed(2));
$('.per').click();
Assuming you already chose from the dropdown it will be fine, otherwise the dropdown wont have a value to calculate with.
Also, the problem with the dropdown is AS soon as I click it closes, i.e. I cant choose anything. To solve this issue I hold the mouse button down, so the dropdown wont close (this is not normal). The reason is because you do dropdown.click() { dropdown.change() } think about replacing this functinality
In my jQuery program, I have 3 textboxes. One for net weight, one for rate and the last readonly text box for amount. The amount textbox calculates the product of the rate and the netweight textboxes and displays the result.
function amtcal(id)
{
var ab="#a"+id;
var ntwt=parseInt($(ab).val(),10);
var a="#"+id;
var rval=parseInt($(a).val(),10);
var product=rval*ntwt;
var abc="#aa"+id;
$(abc).val(product);
}
Check out the working example at jsFiddle.
The program is working but the error that it is causing is that it ignores the decimal values. If I input the following :-Nt wt = 5.5 | Rate = 5.2, the output is given as 25.
Don't use parseInt to convert text to numbers unless the result you want really is an integer. parseFloat or Number will do the job here. ;)
Just remove parseint and let it be rval=$(a).val();
You're using parseInt which will remove all decimal values (making it an int), use parseFloat instead which will keep it (thus making it a float).
Use this JS:
function amtcal(id)
{
var ab="#a"+id;
var ntwt=parseFloat($(ab).val());
var a="#"+id;
var rval=parseFloat($(a).val());
var product=rval*ntwt;
var abc="#aa"+id;
$(abc).val(parseFloat(product).toFixed(2));
}
Here .toFixed() is used to show 2 digits after decimal point.
I have update jQuery code and updated in jsfiddle.
Here is updated jQuery.
function amtcal(id){
var ab="#a"+id;
var ntwt=$(ab).val();
var a="#"+id;
var rval=$(a).val();
var product=rval*ntwt;
var abc="#aa"+id;
$(abc).val(product);
}
i have a table like below which each content is input pop-up
on square i spot you see on last digit have 1,2,8,9 format.
in my html the content of table is value of Posisi
Nomor Rak
<br><input type="text" id="posisi" readonly/></br>
that automaticly i pick using
<td class="data206"><div align="center"><input class="data206" type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this});setvalue(this.value);" value="A1.7.8" /></div></td>
for an example.
so my pop-up like this
my purpose to bind the last digit and then i can manipulate line value. So for an idea script will like below
$(document).ready(function(){
var posisi = $("#posisi").val();
if (posisi== like "1" or like "2" or like "8" or like "9" ){
$("#line").val("9")
}
});
My problem: I don't know how to bind the last digit in jquery..to make conditonal that $("#posisi") last digit value between 1 or 2 or 8 or 9. if this conditional true i can add value in $("#line").val("whatever i want")
need help with an example will be appreciate
Your var posisi will contain a string like "A1.7.8" and you can easily get the last character of string. This may help
How can I get last characters of a string using JavaScript
There are a few more tips which may help.
It seems that you do not want to wrap the code in document.ready. I think you want to get the last digit value on every click. so get the value in "popup_window_show" function or function you are using to show popup.
Moreover if you want to make calculation on that number i.e 1,2,8,9 then convert it into integer form first.
var posisi = $("#posisi").val();
var regex = /\.(\d+)\.?$/;
// Catches the last set of digits following a period.
// Allowing for an additional period at the end.
var matches = posisi.match(regex);
if (matches) {
var lastNumber = parseInt(matches[1], 10);
// Do something
$("#line").val(lastNumber);
}
I have two sliding bars and I want to get the value the user sets them to and do some maths on it. var days and var total are my attempts to do this. I was taking it that the UI slider stores its values as stings and not integers. But both my attempts below, then use either just return a NaN value? So what am I doing wrong?
var days = parseInt($(this).attr('#day_output'), 10);
var total = parseInt($('#amount_outputTotal'));
var fees = 5;
var valueout;
valueout = 60 * days + fees;
$(document).ready(function() {
$('#Output').html(valueout);
});
Many Thanks Glenn.
Why would a slider, which is a representation of numbers, store the value as strings? You can get the value (or values, if two handles) with the following code
var val_single = $('#your_slider').slider('value');
var val_array = $('#your_slider').slider('values');
Please rtfm http://jqueryui.com/demos/slider/
One of our projects using jqueryUI ahs following code:
fixed = $().fixFloat(ui.value);
I understand that this might don't suit your needs.
Try to change from parseInt to parseFloat and also output via console:
$(this).attr('#day_output')
Why here is nothing like .attr or .val or something?
$('#amount_outputTotal')