1

I have an input box whose id is sellingprice. i want to check the input value in jquery using the if else condition and display the result value in an input box whose id is result.

example:

if the value in input box is greater 50 it will display in the other input box the word too high

i'm a newbie in jquery and confused on how to implement it. Appreciate if someone may guide me through this.

I'll revised my question from above:

i have a table value something like this:

sellingprice      result

51                 ?
49                 ?

if the sellingprice value is greater than 50 this will be the calculation: sellingprice - 50 * 0.05 + 3.5

and if the sellingprice value is less than 50 the calculation will be: sellingprice * 0.07;

sorry i'm just trying to simplify my question but this is really the problem that i'm been facing as of this moment. Hope to guide me through this.

3 Answers 3

3

[edit after your update]

var sp = $('#sellingprice').val() | 0;
if (sp <= 50) {
   $('#result').val(sp * 0.07)
}
else {
   $('#result').val(sp - 50 * 0.05 + 3.5) /* check the operations order */
}

where

$('#sellingprice').val() | 0

is an explicit (and more efficient than parseInt) int casting. If you can insert also floated point values change this statement with var sp = parseFloat($('#sellingprice').val())

with a ternary operator the code is simply reduced to

var sp = $('#sellingprice').val() | 0;
$('#result').val((sp <= 50)? sp - 50 * 0.05 + 3.5  : sp * 0.07)
Sign up to request clarification or add additional context in comments.

Comments

1
$('#result').val(
    +$('#sellingprice').val() > 50 ? "too high" : "do backflips!"
);

Comments

0

Try this:

var val = parseFloat($("#sellingprice").val());
var parsedVal = 0;

if (!isNaN(val))
{
  parsedVal = val;
}

if (parsedVal > 50)
{
   $("#result").val("Too High");
}

Comments

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.