Get index in jQuery template

I use the jQuery template plugin and don't know how to get the index of the elements: http://api.jquery.com/category/plugins/templates/

Here is my code:

<script id="optionTmpl" type="text/x-jquery-tmpl"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> {{each Answers}} <tr> <th><input type="radio" name="group1" value="${this.AnswerID}" /></th> <td>${this.AnswerText}</td><!-- add number in this line---> </tr> {{/each}} </table> </script> 

I want to show the answer in a format like the following

1) answer1, 2) answer2, 3) answer3

or

a) answer1, b) answer2, c) answer3

What should I do?

+7
jquery jquery-templates
source share
1 answer

Inside the loop {{each}} there is an implicit $index (and $value ) that you can use here:

 <script id="optionTmpl" type="text/x-jquery-tmpl"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> {{each Answers}} <tr> <th><input type="radio" name="group1" value="${this.AnswerID}" /></th> <td>${this.AnswerText} ${$index + 1}</td> </tr> {{/each}} </table> </script> 

You will probably want to add 1 because it is 0 , as I have above.

+21
source share

All Articles