April 4, 2012

jQuery each number

Question by Ivan C.

I have a idea to make table like this

 1. something1
 2. something2
 3. something3

I have made a table:

<table>
   <tr><td id="rb">...
   <tr><td id="rb">...
</table>

so in #rb is this sequence number starting from 1.

I have this jQuery code but it doesn’t work. Can you pls help 🙂

            $('tr').each(function(index) 
            {
                  $('#rb').append(index);
             });

It just make

       012345

in the first #rb

Thanks in advance!

Answer by Starx

You should not give same id to more than one element within same HTML document. You can do the same using class="rb" instead.

$('tr').each(function(index) {
   $(this).find('.rb').append(index);
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!