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

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>

Related

Show sum of values for all checkboxes that are 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 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

Remove last digit from input type="tel" in HTML [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 5 years ago.
Improve this question
I have full dialpad code .
Clear button clear all text from dialpad window. but i want to delete only last digit which i dial from dialpad.
language only javascript,jquery and html.
function del(){
var num=document.getElementById('num').value;
num = num.substr(0, num.length - 1);
document.getElementById('num').value=num;
}
function isValid(a){
if(!(/^[0-9]+$/.test(a.value))){
a.focus();
console.clear();
console.log("Please enter number");
}
}
<input type="tel" onblur="isValid(this);" id="num" /><button onclick="del();">Del</button>
jQuery - as per requested
$(window).load(function(){
$('button').on('click',function(){
$('#num').val($('#num').val().substr(0,$('#num').val().length-1));
});
});
//optional so not converted to jQuery
function isValid(a){
if(!(/^[0-9]+$/.test(a.value))){
a.focus();
console.clear();
console.log("Please enter number");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel" onblur="isValid(this);" id="num" /><button>Del</button>

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

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

Display price between given dates [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 8 years ago.
Improve this question
Here is the structure and data of my price
if number is between 0 and 2000 (or in other word the number is less then 2000) - then must appear tax "20"
if number is between 2001 and 5000 - then must appear tax 35
Price:<input type=text name=price><br>
Tax: <input type=text name=tax>
First you should add labels for your inputs, they are linked to the input with ids
<label for="priceInput">Price:</label><input id="priceInput" type=text name=price><br>
<label for="taxInput">Tax:</label><input id="taxInput" type=text name=tax>
And in your jquery:
//Function is executed when the priceInput is changed, so when you enter a value
$("#priceInput").change(function(){
//Test if the value of the input is < 2000 and >0
if (($( "#priceInput" ).val()<2000) && ($("#priceInput").val()>0)) {
//Set the value of the tax input to 20
$("#taxInput").val(20)
}
//test if tyhe value is >2000 and <5000
else if(($( "#priceInput" ).val()<5000) && ($("#priceInput").val()>2000)){
$("#taxInput").val(35)
}
else{
$("#taxInput").val("")
}
});
JSFiddle link
function setPrice()
{
if ($('[name=price]').val() < 2000)
{
$('[name=tax]').val(20);
}
else
{
$('[name=tax]').val(35);
}
}

Categories