How to Input a Discount Price in Javascript? - javascript

The Discount field does not register
(discount is supposed to subtract from Grand Total)(check discount sample)
check the the jsfiddle click here
I want it to act like this picture. (with the discount being manually inputed) http://i.dailymail.co.uk/i/pix/2013/02/07/article-2275089-17694138000005DC-460_634x497.jpg
Javascript
function recordToFilename() {
var input = document.getElementById('discountvalue'),
discount12 = input.value;

Change your button to
<input type="button" onclick="recordToFilename();" value="Submit Discount" />
And function
function recordToFilename() {
var input = document.getElementById('discount'),
discount12 = input.value;
alert (discount12);
}

you have 2 id="discount". Change one of the id and the there is no problem to register the input value to discount12.
Put this line document.getElementById('discounts').innerHTML = "<strong>Discount </strong>: $" + (salesTotal * 0.14 + salesTotal - discount12).toFixed(2);
in your recordToFilename() method and make sure your variable is chage to discount12 not discount

Related

Adding a score to every input field the user fills in the form and displaying it

Currently i have developed a program that gets the count of all the inputs field and adding up a percentage for the numbers of fields that are filled individually.
what i need now here, i need to assign a number to each input field and when the user fills an input field i need to show it to the user as a " SCORE ".
below is the program i have built.
<html>
<body>
<label> Name </label>
<input class="track"/>
<label> Name </label>
<input class="track"/>
<h5>Profile Strength <span class='perc'>0</span>%</h5>
</body>
</html>
and the JavaScript is
<script>
$('.track').change(function(e) {
update_progress();
});
// supports any number of inputs and calculates done as %
function update_progress() {
var count = $('.track').length;
var length = $('.track').filter(function() {
return this.value;
}).length;
var done = Math.floor(length * (100 / count));
$('.perc').text(done);
$('.meter').width(done + "%");
}
so when you fill the first input field the 'Profile strength' will show 50% as there are only 2 input fields, and when you fill the second input it will show 100%.
i want to show a number instead of a percentage here like
input 1 = 10
input 2 = 20
so when the user fills input 1 his "SCORE" will be 10,
and when the user fills the next input the total must add on and show 30 in real time.
sorry if have confused a few developers but this the only way i understood the assignment i got.
Try the following:
Html:
<html>
<body>
<label> Name </label>
<input class="track" data-score=10 />
<label> Name </label>
<input class="track" data-score=20 />
<h5>Profile Strength <span class='perc'>0</span>%</h5>
<h5>Score <span id="score">0</span></h5>
</body>
</html>
JS:
$('.track').change(function(e) {
update_progress();
});
// supports any number of inputs and calculates done as %
function update_progress() {
var score = 0
$('input.track').each(function(){
var _score = $(this).data("score")
if ($(this).val().length > 0) {
score += _score
}
})
$('#score').text(score)
var count = $('.track').length;
var length = $('.track').filter(function() {
return this.value;
}).length;
var done = Math.floor(length * (100 / count));
$('.perc').text(done);
$('.meter').width(done + "%");
}
Or, a live version https://jsfiddle.net/wmt6cznh/2/
Is this what you want to achieve?

JS - Calculate values depending on selected dropdowns

So I have this calculator for money that shows you the amount you enter but in a different money value. (Example dollar to euro)
Here is the HTML:
<b> Exchange money </b> <br> <br>
Enter amount for RSD: <input type="number" name="nbsAmount" id="nbsAmount" size="5"> <br>
<button class="dugme">Calculate</button> <br> <br>
Evro value is: <div class="konacnaEvroVrednost"></div>
Dolar value is: <div class="konacnaDolarVrednost"></div>
Swiss value is: <div class="konacnaSwissrednost"></div>
And here is the JS:
$('.dugme').click(function(){
var broj = document.getElementById('nbsAmount').value;
var evro = broj * 0.0085;
var dolar = broj * 0.0095;
var frank = broj * 0.0096;
$('.konacnaEvroVrednost').text(evro + ' €');
$('.konacnaDolarVrednost').text(dolar + ' $');
$('.konacnaSwissrednost').text(frank + ' Fr');
});
And this works fine. As you can see:
Here is the fiddle:
http://jsfiddle.net/5zvdwtpL/1/
But now I want to change this to work a bit more dynamically.
I want there to be two dropdowns that lets you select the value you want to change from to. Like this:
This is what I got so far: https://jsfiddle.net/7s8g9kLt/2/
The problem is that one input value should be copied to the other input value but with the added value of the currency.
So If I select RSD and set 1200, the other USD, then the other input should display 11.4.
So I am stuck a bit here as to how I can achieve this.
First of all, you have bound myFunction to button onClick Event but you have not defined function with this name. You can see following error in console after clicking button
Uncaught ReferenceError: myFunction is not defined
You will have to define this function:
window.myFunction = function() {...}
or event better, add event listener to button click:
document.getElementById('buttonId').addEventListener('click', function() {...})
To calculate dynamic rates, i would first convert input amount to single currency (for example RSD) and then multiply that value by correct rate.
I've modified your jsFiddle (https://jsfiddle.net/rhj4dgz7/3/) to reflect those changes.
You can create a dictionary with the pair of "id" of dropdown and the conversion rate, also you can give the same id to both drop downs. then you gonna just multiply the value by the rate and add the result to the second input.
var rsd = 1;
var evro = 0.0085;
var dolar = 0.0095;
var frank = 0.0096;
var dict = {
"4":rsd,
"1":evro,
"3":dolar,
"2":frank
}
function myFunction(){
var mvs = document.getElementById('mojaVrednostSelect').value;
var nvs = document.getElementById('novaVrednostSelect').value;
var mv = document.getElementById('mojaVrednost').value;
var nv = document.getElementById('novaVrednost').value;
novaVrednost.value = parseInt(mojaVrednost.value) * dict[nvs]
console.log("Yoooo"+ dict[nvs])
console.log("mvs je" + mvs);
console.log("nvs je" + nvs);
console.log("======");
console.log("mojaVrednost je" + mojaVrednost.value);
console.log("novaVrednost je" + novaVrednost.value);
}
document.getElementById('button').onclick = myFunction
check this fiddle
Hope this helps you

How to subtract number from the input box for Grand Total?

Here is my code:
function credit() {
//storing value of grandTotal in txtGrandTotal.
var txtGrandTotal = $("#txtGraTot").val();
//here resetting value of grandTotal to 0.00
var txtGraTotal = document.getElementById('txtGraTot').value = '0.00';
//entering deduction amount
var txtCredit = document.getElementById('txtCreditAmt').value;
//subtracting values
var lclRes = (parseFloat(txtGrandTotal) - parseFloat(txtCredit)).toFixed(2);
//again storing values to Textbox GrandTotal
$("#txtGraTot").val(lclRes);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtCreditAmt" oninput="credit();">
<input type="text" id="txtGraTot">
The result is not as I expected. If I enter 3 in txtCredit input box then value deducting from the Grand Total is 100 - 3 = 97. But if I enter the dot(.) again the result is 97 - . = 94. Why it is happening?
The problem is that you are not storing the original value of grand total in a separate variable. So, when the input event fires, it updates the grand total every time. Here's what is going on:
Grand total Credit
Original state: 100 ___
Press 3 : 97 3 oninput invoked, grand total = (100 - 3)
Press . : 94 3. oninput invoked, grand total = (97 - 3.)
The quick and easy (but not the ideal) solution is to store the original value of grand total in a separate variable, and use that every time input event fires. My solution is to store that value in a data-total attribute ($("#txtGraTot").data("total")) but you can have something else there. Just make sure that you are subtracting from original value, and not from the current value in the text box.
// Save the original value of grand total in an attribute for future use.
if ($("#txtGraTot").data("total") === undefined) {
$("#txtGraTot").data("total", txtGrandTotal);
} else {
// else, get the original grand total value.
txtGrandTotal = $("#txtGraTot").data("total");
}
function credit(){
//storing value of grandTotal in txtGrandTotal.
var txtGrandTotal = $("#txtGraTot").val();
// Save the original value of grand total in an attribute for future use.
if ($("#txtGraTot").data("total") === undefined) {
$("#txtGraTot").data("total", txtGrandTotal);
} else {
// else, get the original grand total value.
txtGrandTotal = $("#txtGraTot").data("total");
}
//here resetting value of grandTotal to 0.00
var txtGraTotal = document.getElementById('txtGraTot').value = '0.00';
//entering deduction amount
var txtCredit = document.getElementById('txtCreditAmt').value;
//subtracting values
var lclRes = (parseFloat(txtGrandTotal) - parseFloat(txtCredit)).toFixed(2);
//again storing values to Textbox GrandTotal
$("#txtGraTot").val(lclRes);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="txtCreditAmt" oninput="credit();">
<br>
<input type="text" id="txtGraTot">
I'm not sure if this is what you want, but that should make sense.
Edited
Add the feature that after you press the enter button, calculate and reset txtCreditAmt.
function credit(){
//subtracting values
var lclRes = (parseFloat($("#txtGraTot").val()) - parseFloat($("#txtCreditAmt").val())).toFixed(2);
//again storing values to Textbox GrandTotal
$("#txtGraTot").val(lclRes);
}
$('#txtCreditAmt').keypress(function(e){
if (e.keyCode == 13) {
credit();
$("#txtCreditAmt").val('');
return false;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtCreditAmt" onblur="credit();">
<input type="text" id="txtGraTot" value="100">

Through javascript decrease amount in a box and increment the same value decrease to another box dynamically

Here is a sample idea.
Amount - discount = amount paid by the customer.
But the customer pay less and the box due need to be increment dynamically.
Can anyone help me please?
Here is a simple version of what I believe you're asking for:
HTML
Price: <input type="text" id="tb-price" />
Discount (%) <input type="text" id="tb-discount" />
Total: <span id="lbl-total" />
Javascript (jQuery required)
$(function(){
// vars
var $tbPrice = $('#tb-price');
var $tbDisc = $('#tb-discount');
var $lblTotal = $('#lbl-total');
// events
$tbPrice.on('input', calc);
$tbDisc.on('input', calc);
// calculation
function calc(){
var x = $tbPrice.val() * ($tbDisc.val() / 100);
var y = $tbPrice.val() - x;
$lblTotal.html(y);
}
});
Example: http://jsfiddle.net/zkhx1z1d/
Do note that there is no validation here and assumes that the user input is clean. Also this should only be used to show the user what they expect to pay, all calculations should be done/verified by the server as suggested by AlienWebguy

find the value of the div then PLUS that numeric value to another div

I have 2 div's.
One is SHIPPING and another is TOTAL PRICE.
<div id="tt_shipping_rate_basket">$0.00</div> = SHIPPING
<div class="op_col5_3" align="right" id="tt_total_basket">$897.00</div> = TOTAL PRICE
Also there are 4 inputs for SHIPPING, each adds it's own price in the shipping div..
for example:
input 1 = 10$
input 2 = 20$
so when choosing an input the value in the #tt_shipping_rate_basket is changing ajax to the value of the input... this works great...
The problem is, I do not want 2 divs (one for shipping and one for total price)...
I just want ONE div, called Total Price, and when I choose an input, the value should ADD&CHANGE to the #tt_total_basket instead of tt_shipping_rate_basket...
What I'm trying to do is, GET the value of SHIPPING field (which is ajax populated), GET the value of TOTAL PRICE field, then just ADD shipping to Total Price...
here is what I tried to write but it doesn't seem to work, sorry I'm not to good at programming..
function plus() {
var spTotal = document.getElementById("tt_total_basket");
var spShip = document.getElementById("tt_shipping_rate_basket");
spTotal = spTotal.innerHTML = +spShip;
}
spTotal is the TOTAL PRICE
spShip is the Shipping Price
The result should be (spTotal = spTotal.value + spShip.value)..
Can somebody please help?
Untested, but you get the idea:
var spTotal = document.getElementById("tt_total_basket").innerHTML.replace('$', '');
var spShip = document.getElementById("tt_shipping_rate_basket").innerHTML.replace('$', '');
spTotal = parseFloat(spTotal) + parseFloat(spShip);

Categories