How to increment?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    How to increment?

    I'm performing an AJAX request where I populate a table. I want to provide a number in each row of the table to indicate where that row lies in relation to the whole table, AKA a column in my table that starts at 1 and increments by 1.

    I know that PHP has something built into it already where you can do something like i++ and it will increment the value for you, however, I'm having trouble implementing this with javascript.

    My code looks like this right now. Can you help me figure out how to ensure that 'i' actually increments?

    Code:
    var RankPlayers = function(obj) {
        var i = 0;
        i++;
        var tr = $( '<tr><td>'+ i +'</td><td>'+obj.name+' ('+obj.position+' '+obj.team+')</td><td>'+obj.value+'</td></tr>' );
        $('#rank').append( tr );
      }
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If you're going to set i to 0 each time, it won't work.

    Here are some suggestions:
    1. Use a "global" variable. This is the quick and easy solution, but you should have one global object and you declare it as a public variable within that.
    2. Use a hidden field to keep the count
    3. Check the number of rows in the table to find out what i should be.

    Comment

    Working...