1

I'm not good in javascript so i was jud experimenting a code. I want to have a multiple condition with different equalization that will subtract a value. this is my sample code but it didn't function, only the first condition functioned.

  var input = document.getElementById("amount");

var first = 30;
var second = 50;
var third = 60;

input.addEventListener("keyup", function () {

    if (input = first) {
       document.getElementById('rebate').value = document.getElementById('amount').value - 2;
     }
    else if (input = second) {
      document.getElementById('rebate').value = document.getElementById('amount').value - 2.50;
     }
    else if (input = third) {
      document.getElementById('rebate').value = document.getElementById('amount').value - 3;
     }
});

function sum() {
        var txtFirstNumberValue = document.getElementById('txt1').value;
        var txtSecondNumberValue = document.getElementById('amount').value;
        var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        if (!isNaN(result)) {
            document.getElementById('total').value = result;
        }
    }

html

 <div class="form-group">
                        <label for="amount">Amount</label>
                        <input type="text" id="txt1"  onkeyup="sum();" value="5" hidden>
                        <input type="text" class="form-control " id="amount" name="amount" placeholder="00" required onkeyup="sum();">
                    </div>

 <div class="row">
                <div class="col-xs-3">
                    <div class="form-group">
                        <label for="total">Total</label>
                        <input type="text" class="form-control" id="total" name="total" placeholder="00" required readonly >
                    </div>
                </div>
            </div>


<div class="row">
                <div class="col-xs-3">
                    <div class="form-group">
                        <label for="total">With Rebate</label>
                        <input type="text" class="form-control" id="rebate" name="rebate" required readonly >
                    </div>
                </div>
            </div>

It would be great guys if you could help me. ^^

3
  • 2
    You can't use = to compare values. You use = to assign values. Commented Apr 14, 2016 at 2:51
  • ahhh okays..but how should i do that? Commented Apr 14, 2016 at 2:54
  • 1
    Use == instead of = to compare values. Commented Apr 14, 2016 at 3:48

1 Answer 1

1

Replace all of your = with == inside the if statements.

Also, var input = document.getElementById("amount") only grabs a reference to the element, not the value.

So in your if statements you need to write input.value == first, etc

Here is a JSFiddle that work: https://jsfiddle.net/bbjpg9np/

  • When the the user types 30, "With Rebate 28"
  • When the the user types 50, "With Rebate 47.5"
  • When the the user types 60, "With Rebate 57"
Sign up to request clarification or add additional context in comments.

3 Comments

Provide your html and we could help you better
Haha, sorry i got to post the fiddle link. My fiddle solves your rebate problem. I guess the sum() is irrelevant now
I've got it sir...thank you soo muchh...it really helps ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.