How to correctly display the numbers for each table row added

I have a slight problem with my application. What is suppose to happen is that when the user selects an option and answer and submits it, it will add a new row. The problem is that the question number is suppose to be +1 every time a new row is created so that it goes question number 1 for row 1, question number 2 for row 2 and etc. Instead it is just displaying the number 1 for each row under the question number column.

How can I get it to display 1,2,3 etc for the question No for each row added.

To use the jsfiddle app do this:

open grid and select option "3", type in the number 1 in the "Number of Answers" text-box and select Button "A". Then click on "Add Question" and a new row is added.

"qnum" in the insertQuestion(form) function controls the question number and the insertQuestion(from) function can be found near the bottom of the javascript code and this function adds the new rows in the table.

Code is in jsfiddle, click here

Thanks

Answers


You were missing an opening <form> tag

forked fiddle: http://jsfiddle.net/ubS6c/


You can use

var qnum = $('#qandatbl').find("tr").length - 1;

You can count the number of elements with a particular class as follows:

var qnum = jQuery('.qid').length;
var html = jQuery("<tr><td class='qid'>" + qnum + "</td></tr>");

Notes:

Typically I would solve this sort of thing with jqgrid and ajax call-backs. Some logical back-end can then be used to do more of the calculation, separating out the separation of interaction, view, control and domain.


Need Your Help

How to limit validation to a particular trigger?

asp.net validation events triggers control

Is there any way to limit the validation of a validator control to a particular event or trigger? Say, I want my validator1 to be activated only when button1 was clicked, and validator2 to be activ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.