I am a total beginner and attempting to make a calculator in Javascript.
When I click on a button I want the value of that button inserted in to a textfield?
Here what i tried and had zero result.
<!-- Html Coding --->
<form name="cal">
<input name="text1" type="text" placeholder="enter value here" /><br/>
<input name="num1" type="button" value="1" onClick="num1()"/>
</form>
<!-- JavaScript coding --->
<script>
function num1()
{
document.cal.text1.value = "1";
}
</script>
Please keep it as simple as possible because im not a native English
function numberClick(value) {
document.cal.text1.value = value;
}
<form name="cal">
<input name="text1" type="text" placeholder="enter value here" /><br/>
<input name="num1" type="button" value="1" onClick="numberClick(this.value)"/>
<input name="num2" type="button" value="2" onClick="numberClick(this.value)"/>
<input name="num3" type="button" value="3" onClick="numberClick(this.value)"/>
</form>
<!-- Html Coding --->
<input name="text1" type="text" placeholder="enter value here" /><br/>
<input name="num1" type="button" value="1" onClick ="num1(this)"/>
<input name="num2" type="button" value="2" />
<input name="num3" type="button" value="3" />
<!-- JavaScript coding --->
<script>
function num1(obj)
{
$('input[name=text1]').val($(obj).attr("value"));
}
</script>
demo
i got this code for calculator with html and javascript, this will help you, use only for reference
<html>
<head>
<script language="javascript">
var flag=0,a,b,c;
function perform(value)
{
if(flag==0)
{
b=parseInt(value);
a=f1.t1.value;
document.f1.t1.value='';
flag=1;
}
else
{
c=f1.t1.value;
document.f1.t1.value='';
if(b==1)
{
var d=parseFloat(a)+parseFloat(c);
document.f1.t1.value=d;
}
else if(b==2)
{
var d=parseFloat(a)-parseFloat(c);
document.f1.t1.value=d;
}
else if(b==3)
{
var d=parseFloat(a)*parseFloat(c);
document.f1.t1.value=d;
}
else
{
var d=parseFloat(a)/parseFloat(c);
document.f1.t1.value=d;
}
flag=0;
}
}
</script>
</head>
<body><center>
<font color="green" size="6" face="elephant"><b><u>CALCULATOR</u></b></font>
</br></br></br>
<table border="2" bordercolor="green" cellpadding="2" >
<tr>
<td><form name="f1">
<input type="text" name="t1" id="txt1">
</form></td></tr>
<tr>
<td><form name="f2">
<input type="button" value=" 1 " onclick="document.f1.t1.value+='1'" >
<input type="button" value=" 2 " onclick="document.f1.t1.value+='2'">
<input type="button" value=" 3 " onclick="document.f1.t1.value+='3'">
<input type="button" value=" + " onclick="perform(1)"></br>
<input type="button" value=" 4 " onclick="document.f1.t1.value+='4'">
<input type="button" value=" 5 " onclick="document.f1.t1.value+='5'">
<input type="button" value=" 6 " onclick="document.f1.t1.value+='6'">
<input type="button" value=" - " onclick="perform(2)"></br>
<input type="button" value=" 7 " onclick="document.f1.t1.value+='7'">
<input type="button" value=" 8 " onclick="document.f1.t1.value+='8'">
<input type="button" value=" 9 " onclick="document.f1.t1.value+='9'">
<input type="button" value=" * " onclick="perform(3)"></br>
<input type="button" value=" 0 " onclick="document.f1.t1.value+='0'">
<input type="button" value=" / " onclick="perform(4)">
<input type="button" value=" = " onclick="perform(value)">
<input type="reset" value= " clr " onclick="document.f1.t1.value='' ">
</form></td>
</tr>
</table>
</center>
</body>
</html>
Related
How do you prevent inputs of more than one operator?
Like to add validation if I press + it will show operation and if I press - since the last character is + should replace it.
And if the last character is + then I press + again, it should not append another plus like ++.
Here is the code:
function ins(val) {
document.getElementById("txtField").value += val
}
function clr() {
document.getElementById("txtField").value = ""
}
function solve() {
let x = document.getElementById("txtField").value
let y = eval(x)
document.getElementById("txtField").value = y
}
body {
background-color: whitesmoke;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h2>Add & Sub by CJ</h2>
<input type="text" id="txtField" readonly>
<br>
<input type="button" value="1" onclick="ins('1')">
<input type="button" value="2" onclick="ins('2')">
<input type="button" value="3" onclick="ins('3')">
<br>
<input type="button" value="4" onclick="ins('4')">
<input type="button" value="5" onclick="ins('5')">
<input type="button" value="6" onclick="ins('6')">
<br>
<input type="button" value="7" onclick="ins('7')">
<input type="button" value="8" onclick="ins('8')">
<input type="button" value="9" onclick="ins('9')">
<br>
<input type="button" value="0" onclick="ins('0')">
<br>
<br>
<input type="button" value="-" onclick="ins('-')">
<input type="button" value="+" onclick="ins('+')">
<input type="button" value="Clear" onclick="clr()">
<input type="button" value="=" onclick="solve()">
<br>
</body>
</html>
You also can try this:)
function ins(val) {
let str = document.getElementById("txtField").value;
let size = document.getElementById("txtField").value.length-1;
if(val!=='+'&&val!=='-'){
document.getElementById("txtField").value += val
}else{
if(size>-1){
if(document.getElementById("txtField").value.charAt(size)!=='+' && document.getElementById("txtField").value.charAt(size)!=='-'){
document.getElementById("txtField").value += val
}else {
if(val==='-'){
if(document.getElementById("txtField").value.charAt(size)==='+'){
document.getElementById("txtField").value=str.substring(0,size)+val
}
}else{
if(document.getElementById("txtField").value.charAt(size)==='-'){
document.getElementById("txtField").value=str.substring(0,size)+val
}
}
}
}
}
}
function clr() {
document.getElementById("txtField").value = ""
}
function solve() {
let x = document.getElementById("txtField").value
let y = eval(x)
document.getElementById("txtField").value = y
}
body {
background-color: whitesmoke;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h2>Add & Sub by CJ</h2>
<input type="text" id="txtField" readonly>
<br>
<input type="button" value="1" onclick="ins('1')">
<input type="button" value="2" onclick="ins('2')">
<input type="button" value="3" onclick="ins('3')">
<br>
<input type="button" value="4" onclick="ins('4')">
<input type="button" value="5" onclick="ins('5')">
<input type="button" value="6" onclick="ins('6')">
<br>
<input type="button" value="7" onclick="ins('7')">
<input type="button" value="8" onclick="ins('8')">
<input type="button" value="9" onclick="ins('9')">
<br>
<input type="button" value="0" onclick="ins('0')">
<br>
<br>
<input type="button" value="-" onclick="ins('-')">
<input type="button" value="+" onclick="ins('+')">
<input type="button" value="Clear" onclick="clr()">
<input type="button" value="=" onclick="solve()">
<br>
</body>
</html>
I updated your code please check now it will work according to your requirements. Happy coding !
var i=-1;var j=-1;
function ins(val) {
i=-1;
j=-1;
var str= document.getElementById("txtField").value += val;
}
function minsins(val) {
j++
if(j==0){
var str= document.getElementById("txtField").value += val;
}
}
function plusins(val) {
i++
if(i==0){
var str= document.getElementById("txtField").value += val;
}
}
function clr() {
document.getElementById("txtField").value = ""
}
function solve() {
let x = document.getElementById("txtField").value
let y = eval(x)
document.getElementById("txtField").value = y
}
body {
background-color: whitesmoke;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h2>Add & Sub by CJ</h2>
<input type="text" id="txtField" readonly>
<br>
<input type="button" value="1" onclick="ins('1')">
<input type="button" value="2" onclick="ins('2')">
<input type="button" value="3" onclick="ins('3')">
<br>
<input type="button" value="4" onclick="ins('4')">
<input type="button" value="5" onclick="ins('5')">
<input type="button" value="6" onclick="ins('6')">
<br>
<input type="button" value="7" onclick="ins('7')">
<input type="button" value="8" onclick="ins('8')">
<input type="button" value="9" onclick="ins('9')">
<br>
<input type="button" value="0" onclick="ins('0')">
<br>
<br>
<input type="button" value="-" onclick="minsins('-')">
<input type="button" value="+" onclick="plusins('+')">
<input type="button" value="Clear" onclick="clr()">
<input type="button" value="=" onclick="solve()">
<br>
</body>
</html>
I am trying to do calculation using jQuery
While subtraction, its always subtracting from the last element
$(".add").click(function(){
$(".sub").val((parseFloat($("#a").val()) - parseFloat($("#paid").val())));
});
$(".add").click(function(){
$(".sub").val((parseFloat($("#b").val()) - parseFloat($("#paid").val())));
});
$(".add").click(function(){
$(".sub").val((parseFloat($("#c").val()) - parseFloat($("#paid").val())));
});
$(".add").click(function(){
$(".sub").val((parseFloat($("#d").val()) - parseFloat($("#paid").val())));
});
<form name="myform">
<input type="button" id="a" value="50">
<input type="button" id="b" value="100">
<input type="button" id="c" value="500">
<input type="button" id="d" value="2000">
</form>
<td><input type="text" name="paid" class="text" id="paid" value="10"/></td>
<td> <input type="button" value = "Calculate" class="add" id="add"></td>
<td><input type="text" class="sub"></td>
when i subtract 10 from 50, it should display 40. but its showing 1990
In your code all the 4 functions are defined inside the add function. When calculate is clicked all functions run one by one and always the value of the ending function is selected. Always subtraction from 2000 takes place due to that always 1990 was the output.
var a;
$('form input').click(function(){a=$(this).val()});
$(".add").click(function(){
$(".sub").val((parseFloat(a)) - parseFloat($("#paid").val()));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="myform">
<input type="button" id="a" value="50">
<input type="button" id="b" value="100">
<input type="button" id="c" value="500">
<input type="button" id="d" value="2000">
</form>
<td><input type="text" name="paid" class="text" id="paid" value="10"/></td>
<td> <input type="button" value = "Calculate" class="add" id="add"></td>
<td><input type="text" class="sub"/></td>
This will help :)
var setVal = 0;
$(".add").click(function() {
console.log("clicked", setVal);
$(".sub").val(setVal - parseFloat($("#paid").val()));
setVal = 0;
});
$("form input").click(function() {
setVal = 0;
setVal = this.value;
console.log("setVal--", setVal)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="myform">
<input type="button" id="a" value="50">
<input type="button" id="b" value="100">
<input type="button" id="c" value="500">
<input type="button" id="d" value="2000">
</form>
<td><input type="text" name="paid" class="text" id="paid" value="10" /></td>
<td> <input type="button" value="Calculate" class="add" id="add"></td>
<td><input type="text" class="sub"></td>
I have a simple calculator with 2 inputs, and when I click on button I want to check if one of inputs is focused and if yes to enter that number.
But when I click on button I lose focus on inputs and get it on that button.
let activeElement = document.activeElement;
function numClick(number){
console.log("Num clicked=" + number);
let tempNumber="";
tempNumber = activeElement.value;
tempNumber = tempNumber + number;
activeElement.value=tempNumber;
}
<input id = "num1" type="text" name="num1" value="">
<span id="operacija"></span>
<input id = "num2" type="text" name="num2" value="">
<span>=</span>
<span id="rezultat">0</span>
<br><br>
<input type="button" onclick="clearIt() "value="C">
<input type="button" onclick="operationClick('/')"value="/">
<br>
<input type="button" onclick="numClick(7)" value="7">
<input type="button" onclick="numClick(8)" value="8">
<input type="button" onclick="numClick(9)" value="9">
<input type="button" onclick="operationClick('*')"value="*">
<br>
<input type="button" onclick="numClick(4)" value="4">
<input type="button" onclick="numClick(5)" value="5">
<input type="button" onclick="numClick(6)" value="6">
<input type="button" onclick="operationClick('-')"value="-">
<br>
<input type="button" onclick="numClick(1)" value="1">
<input type="button" onclick="numClick(2)" value="2">
<input type="button" onclick="numClick(3)" value="3">
<input type="button" onclick="operationClick('+')" value="+">
<br>
<input type="button" onclick="numClick(0)" value="0">
<input type="button" onclick="calculate()" value="=">
<br>
<script type="text/javascript" src="zadatak1.js"></script>
I am not using any framework, just JS...
This is just a working sample of the suggestion given by Teemu. So credit there.
We add a focus event to our input controls and record the last focused control.
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('#num1').addEventListener('focus', setLastElement);
document.querySelector('#num2').addEventListener('focus', setLastElement);
});
var lastInputElement;
const setLastElement = (event) => {
lastInputElement = event.target;
console.log(event.target);
};
let activeElement = document.activeElement;
function numClick(number){
if (lastInputElement === undefined) return;
console.log("Num clicked=" + number);
let tempNumber="";
tempNumber = lastInputElement.value;
tempNumber = tempNumber + number;
activeElement.value=tempNumber;
}
<input id = "num1" type="text" name="num1" value="">
<span id="operacija"></span>
<input id = "num2" type="text" name="num2" value="">
<span>=</span>
<span id="rezultat">0</span>
<br><br>
<input type="button" onclick="clearIt() "value="C">
<input type="button" onclick="operationClick('/')"value="/">
<br>
<input type="button" onclick="numClick(7)" value="7">
<input type="button" onclick="numClick(8)" value="8">
<input type="button" onclick="numClick(9)" value="9">
<input type="button" onclick="operationClick('*')"value="*">
<br>
<input type="button" onclick="numClick(4)" value="4">
<input type="button" onclick="numClick(5)" value="5">
<input type="button" onclick="numClick(6)" value="6">
<input type="button" onclick="operationClick('-')"value="-">
<br>
<input type="button" onclick="numClick(1)" value="1">
<input type="button" onclick="numClick(2)" value="2">
<input type="button" onclick="numClick(3)" value="3">
<input type="button" onclick="operationClick('+')" value="+">
<br>
<input type="button" onclick="numClick(0)" value="0">
<input type="button" onclick="calculate()" value="=">
<br>
<script type="text/javascript" src="zadatak1.js"></script>
You can do something like this:
var activeInput = ""
function inputClick(input){
activeInput = input;
}
function numClick(number){
document.getElementById(activeInput).value += number;
}
<input id="num1" type="text" name="num1" value="" onclick=inputClick('num1')>
<span id="operacija"></span>
<input id="num2" type="text" name="num2" value="" onclick=inputClick('num2')>
<span>=</span>
<span id="rezultat">0</span>
<br><br>
<input type="button" onclick="clearIt() "value="C">
<input type="button" onclick="operationClick('/')"value="/">
<br>
<input type="button" onclick="numClick(7)" value="7">
<input type="button" onclick="numClick(8)" value="8">
<input type="button" onclick="numClick(9)" value="9">
<input type="button" onclick="operationClick('*')"value="*">
<br>
<input type="button" onclick="numClick(4)" value="4">
<input type="button" onclick="numClick(5)" value="5">
<input type="button" onclick="numClick(6)" value="6">
<input type="button" onclick="operationClick('-')"value="-">
<br>
<input type="button" onclick="numClick(1)" value="1">
<input type="button" onclick="numClick(2)" value="2">
<input type="button" onclick="numClick(3)" value="3">
<input type="button" onclick="operationClick('+')" value="+">
<br>
<input type="button" onclick="numClick(0)" value="0">
<input type="button" onclick="calculate()" value="=">
<br>
If you are ready to use Jquery, then you can try this code
let activeElement = document.activeElement;
var prevFocus;
$('input[type="text"]').focus(function() {
prevFocus = $(this);
});
function numClick(number){
console.log("Num clicked=" + number);
let tempNumber="";
tempNumber = activeElement.value;
tempNumber = tempNumber + number;
activeElement.value=tempNumber;
number= prevFocus.val() +''+ number
prevFocus.val(number)
prevFocus.focus()
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id = "num1" type="text" name="num1" value="">
<span id="operacija"></span>
<input id = "num2" type="text" name="num2" value="">
<span>=</span>
<span id="rezultat">0</span>
<br><br>
<input type="button" onclick="clearIt() "value="C">
<input type="button" onclick="operationClick('/')"value="/">
<br>
<input type="button" onclick="numClick(7)" value="7">
<input type="button" onclick="numClick(8)" value="8">
<input type="button" onclick="numClick(9)" value="9">
<input type="button" onclick="operationClick('*')"value="*">
<br>
<input type="button" onclick="numClick(4)" value="4">
<input type="button" onclick="numClick(5)" value="5">
<input type="button" onclick="numClick(6)" value="6">
<input type="button" onclick="operationClick('-')"value="-">
<br>
<input type="button" onclick="numClick(1)" value="1">
<input type="button" onclick="numClick(2)" value="2">
<input type="button" onclick="numClick(3)" value="3">
<input type="button" onclick="operationClick('+')" value="+">
<br>
<input type="button" onclick="numClick(0)" value="0">
<input type="button" onclick="calculate()" value="=">
<br>
This is sample code update. I have kept a track of the last focused on input and changed the text accordingly. I am guessing this is what you asked for. You can write the methods to control the value of the input fields accordingly.
<!-- Added an extra method to remember the last focus, we pass the element as the function param: -->
<input id="num1" type="text" name="num1" value="" onclick='updateFocus(num1)'>
<span id="operacija"></span>
<input id="num2" type="text" name="num2" value="" onclick='updateFocus(num2)'>
<span>=</span>
<span id="rezultat">0</span>
<br><br>
<input type="button" onclick="clearIt() " value="C">
<input type="button" onclick="operationClick('/')" value="/">
<br>
<input type="button" onclick="numClick(7)" value="7">
<input type="button" onclick="numClick(8)" value="8">
<input type="button" onclick="numClick(9)" value="9">
<input type="button" onclick="operationClick('*')" value="*">
<br>
<input type="button" onclick="numClick(4)" value="4">
<input type="button" onclick="numClick(5)" value="5">
<input type="button" onclick="numClick(6)" value="6">
<input type="button" onclick="operationClick('-')" value="-">
<br>
<input type="button" onclick="numClick(1)" value="1">
<input type="button" onclick="numClick(2)" value="2">
<input type="button" onclick="numClick(3)" value="3">
<input type="button" onclick="operationClick('+')" value="+">
<br>
<input type="button" onclick="numClick(0)" value="0">
<input type="button" onclick="calculate()" value="=">
<br>
<script>
var activeElement;
// Updating the current focused on input field:
function updateFocus(input) {
activeElement = input;
console.log("The active element is: +" + activeElement);
}
// Ensuring the number sent on click event is added to the current input box.
function numClick(number) {
// If a button pressed before focusing on any of the inputs:
if (activeElement == null) return;
console.log("Num clicked: " + number);
var tempText = activeElement.value;
tempText += number;
activeElement.value = tempText;
}
</script>
The form code:
<td>
<form action="cart.php" method="get">
<input type="button" onclick="buttonSubtract1()" name="subtract1"
value="-"/>
<input type="text" size="4" id="qty1" name="quantity1" value="0"/>
<input type="button" onclick="buttonAdd1()" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
</form>
</td>
The javascript:
var i = 0;
var qty1 = document.getElementById('qty1').value;
function buttonAdd1() {
document.getElementById('qty1').value = ++i;
}
function buttonSubtract1() {
if (qty1 > 0) {
document.getElementById('qty1').value = --i;}
}
I changed the code to increment and de-increment using javascript which worked fine so I tried to make it so that de-incrementation only works if the number is positive but now incrementing is working fine but it is not allowing de-incrementation of any number. Why is this?
<?php if (isset($_GET['subtract1'])) {
$quantity1 = $_GET['$quantity1'];
$quantity1--;
}
?>
<?php if (isset($_GET['add1'])) {
$quantity1 = $_GET['$quantity1'];
$quantity1++;
}
?>
<form action="cart.php" method="get">
<input type="submit" name="subtract1" value="-"/>
<input type="text" size="4" name="$quantity1" value="<?php echo $quantity1 ?>"/>
<input type="submit" name="add1" value="+"/>
Try the following.
Make sure the php code is above the html (so that the data gets processed before you output it) :
<?php
if(isset($_GET['quantity1'])) {
$quantity1 = intval($_GET['quantity1']);
if(isset($_GET['subtract1'])) {
$quantity1--;
} else if (isset($_GET['add1'])) {
$quantity1++;
}
}
?>
<td>
<form action="cart.php" method="get">
<input type="button" name="subtract1" value="-" />
<input type="text" size="4" name="quantity1" value="<?php echo $quantity1; ?>" />
<input type="button" name="add1" value="+" />
<input type="submit" name="product1" value="Add" />
</form>
</td>
My understanding is to get a reference to the object (qty1) then test or change the properties as required. This code seems to do what is required. It sends the appropriate values as the '$_GET' parameters as checked in PHP.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
<script type = 'text/javascript'>
var rfvGlobal = {};
function buttonAdd1() {
rfvGlobal.qty1.value++;
}
function buttonSubtract1() {
if (rfvGlobal.qty1.value > 0) {
rfvGlobal.qty1.value--;
}
}
</script>
</head>
<body>
<table>
<td><form action="" method="get">
<input type="button" onclick="buttonSubtract1()" name="subtract1" value="-"/>
<input type="text" size="4" id="qty1" name="quantity1" value="0"/>
<input type="button" onclick="buttonAdd1()" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
</form></td>
</table>
<script>
rfvGlobal.qty1 = document.getElementById('qty1');
</script>
</body>
</html>
View this working demo
Your problem is that you aren't checking that the element's value, just qty1 which was set on page load but never updated.
function buttonSubtract1() {
var qtyElement = document.getElementById('qty1'); // the element
if (qtyElement.value > 0) qtyElement.value = --i; // if its value at this point in time >0 change it
}
The above fixes your current code, but...
Why don't you write less (this is the only function you need)
function buttonAlter(which, byWhat){
var qtyElement = document.getElementById('qty' + which);
if (byWhat > 0 || qtyElement.value > 0) qtyElement.value = parseInt(qtyElement.value) + byWhat;
}
for more buttons
<form action="cart.php" method="get">
<input type="button" onclick="buttonAlter(1, -1)" name="subtract1" value="-"/>
<input type="text" size="4" id="qty1" name="quantity1" value="0"/>
<input type="button" onclick="buttonAlter(1, 1)" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
<br />
<input type="button" onclick="buttonAlter(2, -1)" name="subtract1" value="-"/>
<input type="text" size="4" id="qty2" name="quantity1" value="0"/>
<input type="button" onclick="buttonAlter(2, 1)" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
<br />
<input type="button" onclick="buttonAlter(3, -1)" name="subtract1" value="-"/>
<input type="text" size="4" id="qty3" name="quantity1" value="0"/>
<input type="button" onclick="buttonAlter(3, 1)" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
</form>
This version's demo
I have dynamically showing product list with price and quantity in text box, i want to calculate product total price when enter the quantity.I have done this by onclick when increase or decrease button.but i also need to calculate the total price when key up quantity textbox
My code is like
<div id="gw_articles">
<div id="article_list">
<p>
<div id="article_1_element">
<input type="text" id="jform_article_1_name" name="jform[article_1][name] " value="Spark plug ARH-II AIW16 IRIDIUM+" disabled="disabled" size="35" /> <a id="jform_article_1_modal">Select an product</a>
<input type="hidden" id="jform_article_1_id" name="jform[article_1][id]" value="2" />
<input type="hidden" name="jform[article_1][price]" id="jform_article_1_price" value="30.00" class="inputbox" />
<input type="text" name="jform[article_1][price_total]" id="jform_article_1_price_total" value="90" class="inputbox" size="10" />
<input type="text" name="jform[article_1][quantity]" id="jform_article_1_quantity" value="3" class="inputbox" size="10" />
<input type="button" class="btn" value="+" onclick="document.id('jform_article_1_quantity').value++; calculateTotalPrice('jform_article_1')" />
<input type="button" class="btn" value="-" onclick="if(document.id('jform_article_1_quantity').value >1)document.id('jform_article_1_quantity').value--; calculateTotalPrice('jform_article_1')" />
<a title="test" class="btn btn-danger" onclick="removeElement('article_1_element')"> Delete </a>
</div>
</p>
<p>
<div id="article_2_element">
<input type="text" id="jform_article_2_name" name="jform[article_2][name] " value="Spark plug ARH-II AIW20 IRIDIUM+" disabled="disabled" size="35" /> <a id="jform_article_2_modal" class="btn modal" title="Select an product">Select an product</a>
<input type="hidden" id="jform_article_2_id" name="jform[article_2][id]" value="1" />
<input type="hidden" name="jform[article_2][price]" id="jform_article_2_price" value="40.00" class="inputbox" />
<input type="text" name="jform[article_2][price_total]" id="jform_article_2_price_total" value="40" class="inputbox" size="10" />
<input type="text" name="jform[article_2][quantity]" id="jform_article_2_quantity" value="1" class="inputbox" size="10" />
<input type="button" class="btn" value="+" onclick="document.id('jform_article_2_quantity').value++; calculateTotalPrice('jform_article_2')" />
<input type="button" class="btn" value="-" onclick="if(document.id('jform_article_2_quantity').value >1)document.id('jform_article_2_quantity').value--; calculateTotalPrice('jform_article_2')" />
<a title="test" class="btn btn-danger" onclick="removeElement('article_2_element')"> Delete </a>
</div>
</p>
</div>
<input type="hidden" id="articles_max" name="articles_max" value="2" />
</div>
So much going on in this code goodness. Try this:
function calculateTotalPrice(article) {
var price = and * multiplication - (some + addition);
$('#jform_article_2_total_price').val(price);
}
$('#jform_article_2_quantity').focusout(function(){
calculateTotalPrice('jform_article_2');
});
I sloved it by
`jQuery(document).ready(function($) {
jQuery("input[id*='quantity']" ).focusout(function(){
max_value = $("#articles_max").val();
var n =1;
jQuery("input[id*='quantity']" ).each(function(){
if (n <= max_value) {
id='jform_article_'+n;
calculateTotalPrice(id);
n++;
}
})
})
});
function calculateTotalPrice(field){
var price = document.id(field + "_price").value;
var quantity = document.id(field + "_quantity").value;
document.id(field + "_price_total").value = price * quantity;
}`