0

i got a group of inputs... each one has a number value.
i want to get all their values (found a method here) and then compare then and highlight the heighest input meaning highlight the input itself
meaning i need to somehow grab its id and know which one i am comparing to...

(i hope i explained it good).

This is what i have for now taken from the link attached:

var values = [];
$("input[name='items[]']").each(function() {
    values.push($(this).val());
});
Community
  • 1
  • 1
Sagive
  • 1,699
  • 1
  • 24
  • 34

3 Answers3

1
var highestVal = 0,
    $target;
$("input[name='items[]']").each(function() {
    if(parseInt($(this).val()) > highestVal){
      highestVal = parseInt($(this).val());
      $target = $(this);
    }
});

// $target is now the input with the highest value
ahren
  • 16,803
  • 5
  • 50
  • 70
  • Thanks for your help. i actually stated that i need the ID also and @rajeshkakawat solution worked almost "out the box" but i apreaciate the time you spent. – Sagive Oct 22 '13 at 07:00
1

try something like this

        $(function(){
            var higesht_val = 0;
            var higesht_val_id = 0;
            $("input[name='items[]']").each(function() {
                var current_val = parseInt(this.value);
                if(higesht_val < current_val){
                    higesht_val = current_val;
                    higesht_val_id =  this.id;
                }
            });

            alert(higesht_val); // highest value
            alert(higesht_val_id);// id of  highest value input
        })
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
  • 1
    What if the elements don't have IDs? – ahren Oct 22 '13 at 06:26
  • @ahren meaning i need to somehow grab its id and know : Sagive SEO.So it is sure elem have id – rajesh kakawat Oct 22 '13 at 06:31
  • Yee... it has an id - what i need to higlight it (attach a class) is the id for other calculations.. Thanks - Runing a test on your answer now ;) – Sagive Oct 22 '13 at 06:55
  • Cool! this works! - i only had to add a parseInt so the check would work <-- please edit your answer for other people - instead of higesht_val = this.value; its actually higesht_val = parseInt(this.value); Thanks a lot for your help, nice touch to integrate the alerts (time saver). – Sagive Oct 22 '13 at 06:59
0

how about this ?

var values = [];
$("input[name='items[]']").each(function() {values.push(this);});
values.sort(function(a, b){return  b.value - a.value;})
highlight(values[0]);
cave
  • 11
  • 1