Dominic
Dominic

Reputation: 341

How to increment or decrement the Value from defined OLD value

enter image description hereThe Value is passed to C-(running-summary-total) from A OR B then that value should be passed to D-(wordcount) and increase or decrease, starting from that defined value, Let say the value is 150 then the increment or decrement should start from that 150. enter image description here

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="number" id="wordcount" onchange="calTotal();" name="wordcount" value="" min="1"
    max="100000000000">

<input class="form-control" type="number" id="quantity" onchange="calTotal();" name="quantity" value="" min=""
    max="100000000000">

<input type="number" readonly="readonly" min="1n" name="total" id="running-summary-total" value="0.00">

JAVASCRIT

function calTotal() {
    let wordcount = $("#wordcount").val(); //get wordcount value
    let quantity = $("#quantity").val(); //get quantity value
    if (wordcount) { //if wordcount has value
        var wc = wordcount * 0.098;
        $("#running-summary-total").val(wc);
    }
    if (quantity && quantity < 0) { //if quantity has value and its smaller then 0 (negative value)
        let oldtotal = $("#running-summary-total").val(); //getting the old total value
        let calc = Math.abs(quantity) * 2; //added Math.abs to turn negative number into positive, and mulitply by 2
        let newtotal = oldtotal / calc; // here we divide the old total by the quatity times 2
        $("#running-summary-total").val(newtotal);
    }
    if (quantity && quantity > 0) { //if quantity has value and its bigger then 0 (positive value)
        let oldtotal = $("#running-summary-total").val(); //getting the old total value
        let calc = quantity * 2; //here we multiply quantity by 2
        let newtotal = oldtotal * calc; // here we multiply the old total by the quantity times 2
        $("#running-summary-total").val(newtotal);
    }
}

ISSUE

The issue or problem i am getting is that the value does not increase or decrease from the value, kindly help

Upvotes: 1

Views: 168

Answers (1)

Always Helping
Always Helping

Reputation: 14570

Here you go. Working code you.

A little tricky scenarios just i finally got after reading your question multiple times. To archive what you want: Assign you A or B value to a var = oldValue on select and display in the running-summary-total.

Once you update your word count it will calculate the value from the running-summary-total and * 0.098 as you wanted.

Working Demo: https://jsfiddle.net/usmanmunir/8ersy4mk/22/

Run snippet below to see it working. Select A or B and do the rest as you want to!

let oldValue = 0

function getAorB(event) {
  oldValue = event.value
  $("#running-summary-total").val(oldValue);
}

function calTotal() {
  let wordcount = $("#wordcount").val(); //get wordcount value
  let quantity = $("#quantity").val(); //get quantity value
  if (wordcount) { //if wordcount has value
    var wc = wordcount * .098;
    var total = parseInt(oldValue) + wc
    $("#running-summary-total").val(total);
  }
  if (quantity && quantity < 0) { //if quantity has value and its smaller then 0 (negative value)
    let calc = Math.abs(quantity) * 2; //added Math.abs to turn negative number into positive, and mulitply by 2
    let newtotal = total / calc; // here we divide the old total by the quatity times 2
    $("#running-summary-total").val(newtotal);
  }
  if (quantity && quantity > 0) { //if quantity has value and its bigger then 0 (positive value)
    let calc = quantity * 2; //here we multiply quantity by 2
    let newtotal = total * calc; // here we multiply the old total by the quantity times 2
    $("#running-summary-total").val(newtotal);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input type="radio" id="a" name="gender" value="100" onclick="getAorB(this)">
<label for="a">Select A</label><br>
<input type="radio" id="b" name="gender" value="150" onclick="getAorB(this)">
<label for="b">Select B</label><br>

<br> D <input class="form-control" type="number" id="wordcount" onchange="calTotal();" name="wordcount" value="" min="1" max="100000000000"> E <input class="form-control" type="number" id="quantity" onchange="calTotal();" name="quantity" value="" min=""
  max="100000000000"> C <input type="number" readonly="readonly" min="1n" name="total" id="running-summary-total" value="0.00">

Upvotes: 1

Related Questions