Show sum of values for all checkboxes that are checked [closed] - javascript

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

Related

Get value from selected Checkbox value using Java Script/JQuery [closed]

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 2 years ago.
Improve this question
I need to get selected dynamically generated checkbox value using Javascript/jQuery.
Below the code I am using. I need to get id from selected checkbox.
function GetSelectedId() {
var array = []
$("input:checkbox[name=type]:checked").each(function() {
alert(array.push($(this).val()));
});
}
<td><input type="checkbox" name="type" id="400" /> </td>.
<td><input type="checkbox" name="type" id="401" /> </td>
You can get the inputs fields with the name type which are checked in JQuery by
$('input[name="type"]:checked');
Then you can map the id's to a new array
inps.map(x => x.id)
Hence you can use map you have to make the result of the query iterable by
let inps = [...$('input[name="type"]:checked')];
Then you can return the array in your function
function getSelectedId() {
// ...
return inps.map(x => x.id)
}
Note: Function names usually starts with a lowercase letter
function getSelectedId() {
let inps = [...$('input[name="type"]:checked')];
return inps.map(x => x.id)
}
const arr = getSelectedId();
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td><input type="checkbox" name="type" id="400" checked/> </td>.
<td><input type="checkbox" name="type" id="401" /> </td>

Divide a span value by a static number - JS [closed]

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 />

Subtract 1 from total if check box is checked [closed]

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 6 years ago.
Improve this question
Say you have a checkbox and beside it you show any numbers, be it 10 and when user clicks on the checkbox then that 10 becomes 9 but if unchecks then it again becomes 10.
<input type="checkbox" id="credits" name="credits" <?php echo $checked;?> /> (10) Credit
The value of credit would be fetched from database.
$(function () {
$('#credits').change(function () {
var currentValue = parseInt($('#credit-amount').text());
var newValue = currentValue + ($(this).prop('checked') ? -1 : 1);
$('#credit-amount').text(newValue);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="credits" name="credits" /> (<span id="credit-amount">10</span>) Credit

How to make a special auto-subtractor? [closed]

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>

javascript to multiply product price and quantity [closed]

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)
};

Categories