0

I have one input in which you can enter value, that gets checked and if it is greater than number in if statement, it gets value of percentage in second, disabled field...

Now the problem came out when I tried to check it on different numbers, with else if and else statements. I'm jQuery nooby and I don't know how this can be solved..

So far I have this situation: http://jsfiddle.net/dzorz/xFHte/

html:

<input type="text" class="entered" id="entered" name="entered">
<input type="text" class="percent" id="percent" name="percent" disabled>

script:

$(".entered").change(function(){

 if(parseFloat(this.value) > 39617756.85){
    $(".percent").val('1%');
 }


 else if(parseFloat(this.value) > 19205010.98){
    $(".percent").val('2%');
 }


 else if(parseFloat(this.value) > 12378426.86){
    $(".percent").val('3%');
 }

 else(parseFloat(this.value) > 1242179.25){
    $(".percent").val('not qualified');
 }

});

what am I doing wrong?

3
  • 2
    There's a syntax error in your code, last if is missing, jsfiddle.net/xFHte/2 Commented Jun 27, 2013 at 13:53
  • Not the problem, but may I suggest a minor optimisation/tidy-up: avoid repeated parseFloat() calls by adding var val = parseFloat(this.value) at the beginning of your function and then in each if just test val... Commented Jun 27, 2013 at 13:55
  • Maybe I missed it, but what exactly is the problem. What number does it fail on. What do you input and what is the problematic output. You have all these magic numbers, I'm not understanding this. Commented Jun 27, 2013 at 13:57

2 Answers 2

2

I think the problem is you need the last 'not qualified' in a else block not in a else if so that if none of the condition is met the `not qualified message will be displayed.

$(".entered").change(function(){
    var value = parseFloat(this.value);
    if(value > 39617756.85){
        $(".percent").val('1%');
    } else if(value > 19205010.98){
        $(".percent").val('2%');
    } else if(value > 12378426.86){
        $(".percent").val('3%');
    } else{
        $(".percent").val('not qualified');
    }
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

You had some problems with your html syntax

<input type="text" class="entered" id="entered" name="entered" />
<input type="text" class="percent" id="percent" name="percent" disabled="disabled" />

Combined with the suggestions in the comments

$(".entered").change(function () {
    var value = parseFloat($(this).val());
    var text = "";

    if (value > 39617756.85) {
        text = "1%";
    } else if (value > 19205010.98) {
        text = "2%";
    } else if (value > 12378426.86) {
        text = "3%";
    } else if (value > 1242179.25) {
        text = "not qualified";
    }

    if (text != "") $(".percent").val(text);
});

Will give you this working example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.