0

I have a table-structure like bellow :

function highestPosInBlock(posId) {
  // determine the highest number in the current block
  console.log($("body").data("block")); // ...
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
  <tr>
    <td data-position="3">3</td>
  </tr>
</table>

<table data-block="2">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
</table>

I want to retrieve the highest data-position attribute of a given Block. My JS-Function so far looks like this:

function highestPosInBlock(posId) {
    // determine the highest number in the current block
    console.log($("body").data("block") // ...
}

and this is exactly where I'm stuck. The argument I pass is the given data-block. For instance, I'd call the function like this:

highestPosInBlock(1) I want to retrieve 3

In case I'd call highestPosInBlock(2) it shoudl return 2

Any idea how to do so? I have a complete blackout.

Another thing: I can't use the each-iterator in jQuery, since this shit has to work in IE 7, which doesn't support each()

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
DasSaffe
  • 2,080
  • 1
  • 28
  • 67
  • First get the table you want: `$('table [data-block="1"]')` then [find](https://api.jquery.com/find/) the tds and loop through [each](https://api.jquery.com/each/) of them, [get the data-position](http://stackoverflow.com/questions/5309926/how-to-get-the-data-id-attribute) and [keep track of the highest value](http://stackoverflow.com/questions/28767330/javascript-biggest-number-from-array-using-loop) – musefan May 18 '17 at 12:21
  • @DasSaffe added for loop to prevent use each() as you said in your question – Bourbia Brahim May 18 '17 at 12:57

1 Answers1

1

Bellow by example we get the max postion of the block 1 ,

and this was done by getting the table (by its data-block attribute ) then looping throw it's td (using .find("\[data-position\]")) then loop throw those and get the max value of them .

Here is a working snippet :

function highestPosInBlock(posId) {
    // determine the highest number in the current block
   console.log($("[data-block="+posId+"]")) 
   var max = null;
   $("[data-block="+posId+"]").find("[data-position]").each(function(){
        var value = parseInt($(this).attr('data-position'));
       max = (value > max) ? value : max;
    });
    
    console.log(max);
    alert("Max position value of the table with data-block = "+posId+" is -> "+max)
}



highestPosInBlock(1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
    <tr>
        <td data-position="1">1</td>
    </tr>
    <tr>
        <td data-position="2">2</td>
    </tr>
    <tr>
        <td data-position="3">3</td>
    </tr>
</table>

<table data-block="2">
    <tr>
        <td data-position="1">1</td>
    </tr>
    <tr>
        <td data-position="2">2</td>
    </tr>
</table>

If you want to prevent using the find() use for loop as bellow :

function highestPosInBlock(posId) {
    // determine the highest number in the current block
    var max = null;
    var tds = $("[data-block="+posId+"]").find("[data-position]");
   
    if(tds.length>0){
       for(var i=0;i<tds.length;i++) {
         var value =  parseInt($(tds[i]).attr('data-position'));
         max = (value > max) ? value : max;
       }
    }
    
    console.log(max);
    alert("Max position value of the table with data-block = "+posId+" is -> "+max)
}



highestPosInBlock(1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
  <tr>
    <td data-position="3">3</td>
  </tr>
</table>

<table data-block="2">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
</table>
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52