469,295 Members | 1,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

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?

Expand|Select|Wrap|Line Numbers
  1. var RankPlayers = function(obj) {
  2.     var i = 0;
  3.     i++;
  4.     var tr = $( '<tr><td>'+ i +'</td><td>'+obj.name+' ('+obj.position+' '+obj.team+')</td><td>'+obj.value+'</td></tr>' );
  5.     $('#rank').append( tr );
  6.   }
Mar 5 '11 #1
1 1799
16,027 Expert Mod 8TB
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.
Mar 5 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Mark Turney | last post: by
2 posts views Thread by Tom | last post: by
13 posts views Thread by kailasam | last post: by
1 post views Thread by Krish | last post: by
3 posts views Thread by George Ter-Saakov | last post: by
15 posts views Thread by Ryan Liu | last post: by
11 posts views Thread by divya_rathore_ | last post: by
8 posts views Thread by bintom | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.