Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I write javascript code to multiply product price to the quantity input in the input field but it's not working can somebody have any idea where is the problem..
javascript
<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.abc.QTY.value);
b=Number(document.abc.PPRICE.value);
c=a*b;
document.abc.TOTAL.value=c;
}
</script>
form
<input type="text" value="" name="QTY" id="QTY" onKeyUp="multiply()" /><br />
<input type="text" name="PPRICE" id="PPRICE" value="<?php echo $product['pprice']; ?>" /><br />
<input type="text" name="TOTAL" id="TOTAL" /><br />
I'm not quite sure where you're getting abc from but you should be using document.getElementById(). Change your function code to the following and all will be well.
function multiply()
{
// Get the input values
a = Number(document.getElementById('QTY').value);
b = Number(document.getElementById('PPRICE').value);
// Do the multiplication
c = a*b;
// Set the value of the total
document.getElementById('TOTAL').value=c;
}
function hitungTotal(){
var qty = document.getElementById("newQuantity").value;
var price ="";
var totPrice =qty * price;
document.getElementById("newTotal").value = totPrice;
}
function total() {
let qty = $('.qty').val();
let price = $('.price').val()
$('.total').val(qty * price)
};
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a field in a table which takes in a value from an input field
<td>€<span class="totalNumber"></span></td>
I have another field that has a static number so example 50%.
The result I want is the third field to have totalNumber divided by .50 so my final field be my result.
Example:
totalNumber takes in the value 100.
Second field is static 50%
So my result field will be 50
I've tried using: var value = Math.floor(#totalNumber * .50);
I'm not sure if that could be used or my syntax is just wrong.
You will need simple javascript.
var num = parseInt($('span.totalNumber').text());
var staticnum = parseInt($('span.staticNumber').text());
var answer = (num * staticnum)/100;
$('span.result').text(answer);
var num = parseInt($('span.totalNumber').text());
var staticnum = parseInt($('span.staticNumber').text());
var answer = (num * staticnum)/100;
$('span.result').text(answer);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Total Number : <span class="totalNumber">200</span>
</div>
<div>
Static Number : <span class="staticNumber">50%</span>
</div>
<div>
Result : <span class="result"></span>
</div>
Your provided code is to meager to provide a good answer but here is an example how you can do something like that.
At total you enter the number you want to divide.
At percent you add the percentage.
Both these inputs have a change event bound to them with the same handler so that if any of these changes it executes the handler. You probably want to check too if not both inputs are empty.
var total = document.getElementById('totalValue').addEventListener('change', calculate);
var percent = document.getElementById('percentValue').addEventListener('change', calculate);
function calculate() {
var total = document.getElementById('totalValue');
var percent = document.getElementById('percentValue');
var calc = document.getElementById('calculatedValue');
calc.value = total.value * (percent.value / 100)
}
<label for="totalValue">Total</label>
<input id="totalValue" type=text />
<br />
<label for="percentValue">Percent</label>
<input id="percentValue" type=text />
<br />
<label for="calculatedValue">Calculated</label>
<input id="calculatedValue" type=text />
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I’m creating an HTML form and want to achieve the following with JS, please provide the code i should use to do so.
1.Add values of all the checked checkboxes and show them as total.
2.Add a restriction the user must select at least 2 checkboxes.
Here is my code.
<input class="iput" type="checkbox" name="E33" value="4500" />
<input class="iput" type="checkbox" name="E34" value="3000" />
<input class="iput" type="checkbox" name="E36" value="6000" />
<p>Your Total is = </p>
Also code should be such that if i add or remove checkboxes i should not have to modify the JS code too.
Get all the checkbox using document.getElementsByClassName.This will give a collection. Using ... spread operator you can convert it to array and use forEach to loop over them and add an change event listener. and update the total on it's change.The value of the checkbox is string so using parseInt convert it to number
let total = 0;
[...document.getElementsByClassName('iput')].forEach(function(item) {
item.addEventListener('change', function(e) {
if (e.target.checked) {
total += parseInt(e.target.value, 10)
} else {
total -= parseInt(e.target.value, 10)
}
document.getElementById('total').innerHTML = total
})
})
<input class="iput" type="checkbox" name="E33" value="4500" />
<input class="iput" type="checkbox" name="E34" value="3000" />
<input class="iput" type="checkbox" name="E36" value="6000" />
<p id="total">Your Total is = </p>
Assuming you only have inputs of type checkbox.
var checkboxes = document.getElementsByTagName('input');
var sum = 0;
checkboxes.forEach(function(checkbox){
if (checkbox.checked) {
sum += checkbox.value;
}
});
console.log(sum); // your sum
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make with JS, a subtraction which auto-subtract the biggest number from the smallest one... How to do so ? Thanks
Using HTML5, if you listen for the oninput event of two <input type="number" /> fields, you can call Math.abs() on the difference between the two numbers, and it will update constantly.
Here's a small demo:
var input1 = document.getElementById("firstNum"),
input2 = document.getElementById("secondNum"),
output = document.getElementById("output");
input1.oninput = input2.oninput = function() {
var num1 = parseInt(input1.value),
num2 = parseInt(input2.value);
output.innerHTML = Math.abs(num1 - num2);
};
Input 1: <input type="number" value="0" id="firstNum" /><br>
Input 2: <input type="number" value="0" id="secondNum" /><br>
Output: <span id="output">0</span>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to sum 6 inputs and set the value to another input with javascript.
https://jsfiddle.net/arispapapro/1qbjd36c/9
<form>
<input type="text" name="no301" class="form-control" id="no301" placeholder="">
<input type="text" name="no301" class="form-control" id="no302" placeholder="">
<input type="text" name="no301" class="form-control" id="no303" placeholder="">
<input type="text" name="no301" class="form-control" id="no304" placeholder="">
<input type="text" name="no301" class="form-control" id="no305" placeholder="">
<input type="text" name="no301" class="form-control" id="no306" placeholder="">
<input type="text" name="no307" class="form-control" id="thesum" placeholder="307">
</form>
Javascript:
var no301 = document.getElementById("no301").value;
var no302 = document.getElementById("no302").value;
var no303 = document.getElementById("no303").value;
var no304 = document.getElementById("no304").value;
var no305 = document.getElementById("no305").value;
var no306 = document.getElementById("no306").value;
var no307 = document.getElementById("no307").value;
var sum = no301 + no302 + no303 + no304 + no305 + no306;
sum.onchange = function() {
thesum.value = sum;
}
thesum.onchange = function() {
sum.value = thesum;
}
Check the fiddle: https://jsfiddle.net/1qbjd36c/13/
$("form .form-control").not("#thesum").on("input", function() {
var getSum = 0;
$("form .form-control").not("#thesum").filter(function() { if($.isNumeric($(this).val())) return $(this).val(); }).each(function() {
getSum+=parseFloat($(this).val());
});
$("#thesum").val(getSum);
});
$("form .form-control") A tag and class selector has been utilized to reference the target.
not("#thesum") added a not selector in order to avoid the change of input of Resulting TEXT field.
on("input", function() { utilized ON-INPUT event, to trigger all input formats, which includes paste of clip text too.
.filter(function() { utilized filter function to value only numeric values.
getSum+=parseFloat($(this).val());, here use of + indicates summing upon with the previous value to the variable, in other words, recursive change on value, which returns the sum of all iterated values.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Please help me in this issue
I want to fill all the fields with random data
https://jsfiddle.net/omrmstg7/
<html>
<head>
<script>
//<![CDATA[
window.onload=function(){
var button = document.getElementById("my-button");
var input = document.getElementById("my-input");
var names = ["Henry", "Joseph", "Mark", "Michael"];
button.addEventListener("click", function() {
input.value = names[Math.floor(Math.random() * names.length)];
});
}//]]>
</script>
</head>
<body>
<button id="my-button">Generate Random Names</button>
<input type="text" id="my-input" />
<input type="text" id="my-input" />
<input type="text" id="my-input" />
</body>
</html>
I'm guessing your problem is that you want to fill all the inputs and it doesn't do that.
The problem is that id is reserved for unique elements.
So, if you change your HTML for id to be class instead, you would change your JavaScript to something like this:
var button = document.getElementById("my-button");
var inputs = document.getElementsByClassName("my-input");
var names = ["Henry", "Joseph", "Mark", "Michael"];
button.addEventListener("click", function() {
Array.prototype.forEach.call(inputs, function (input) {
input.value = names[Math.floor(Math.random() * names.length)];
});
});
Change the inputs to use a class instead of an id.
id values should be unique in HTML and getElementById only returns the first matching id.