You are viewing limited content. For full access, please sign in.

Question

Question

Forms Javascript - counting rows in a collection, not working with tbody tr or ul

asked on October 4, 2018

I can count the rows in a table with this line of code.

    var count = $('.table tbody tr').length;

I have tried the same thing for a collection, but I seem to get a count of the total number of fields in the collection instead.

So I tried digging through the HTML and finding something besides tbody tr and found both ul and hr appeared to work. For example:

var count = $('.collection hr').length;

var count = $('.collection ur').length;

However, as soon as you add fields to the collection, you get strange results for your counts. ur returns a much higher number than expected and hr returns fractions of a number.

Does anyone know what the unique, if any, identifier for a new row is?

0 0

Answer

SELECTED ANSWER
replied on October 4, 2018

Hey Chad,

 

If you're trying to count the number of sets of fields in the collection, you'll probably want to use $(".collection div.form-q").length to count that.

 

If you're trying to get the total number of fields, you should be able to use $(".collection div.form-q li").length to get that count.

0 0
replied on October 4, 2018 Show version history

Forms includes built-in counters for tables and collections via a hidden input field.

For Example,

$('.myTable .propCount').val()

$('.myCollection .propCount').val()

Would return the current number of rows/items without relying on .length()

4 0
replied on October 9, 2018

Nice, thank you. It works and is cross compatible.

0 0

Replies

replied on October 4, 2018

Are you trying to count the number of fields or the number of sets of fields?

0 0
replied on October 4, 2018

Thanks guys! $(".collection div.form-q").length was what I was looking for to count the sets (what I was thinking of as rows in a table).

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.