Search code examples
phpjquerytextboxwhile-loopminimum

Compare values of text boxes in loop and highlight the minimum


I need some help from you all..

Actual Scenario is : This is Module to Update today's price of products and there are seven TextBoxes created in loop with

<input type="text" id="<?="rate2".$i?>" name="<?="rate2".$i?>" size="5" />

($i is increment variable of loop) also I already got abc rate (Standard price).

Now Problem is : Whenever user enters the value on 1 TBX it should compare this.value with abc rate and if this.value is low then entire row's text color should be red, so on. then finally, it should also highlight the lowest of all.

Please help me in this asap Thanks in advance...


Solution

  • using jQuery it could be something like this :

    <input type="text" id="<?="rate2".$i?>" name="<?="rate2".$i?>" size="5" 
    onkeypress="if( parseInt($(this).val()) < abc )$(this).css('background-color','red')"/>
    

    Add a function at Prototype :

    Array.prototype.min = function() {
        var r = this[0];
        this.forEach(function(v,i,a){if (v<r) r=v;});
        return r;
    };
    
    var items = [];
    $('input:test').each(function(){
       items.push($(this).val());
    });
    
    var lowest = items.min;
    
    $("input:text[value='"+min+"']").css('background-color','blue');