Jump to content

Counting the number of options in a Dropdown

Recommended Posts

Hi there,


I have a Submission Form DataPage and I wanted to like count the number of options in a Dropdown field. I would be using that for division as I wanted to make a report on statistics.


I already have some JavaScript working but the problem is, it doesn't work on Dropdowns with more than 100 options.

document.addEventListener("DataPageReady", function() {
var count = document.querySelector('[id=*InsertRecordCategory]').length;

What's weird is that it returns a number (e.g. 7) when less than 100 or 0 when greater than 100 or so.


Hope someone can help.

Link to comment
Share on other sites

Hello @TroubleShooter,


The most likely reason why this happens is that DataPageReady fires when the DataPage DOM has finished loading, and a Dropdown with a lookup table loads only after its part in the DOM is loaded.

Performance-wise, if the lookup takes a tiny second to load, it catches up before the DataPageReady event is fired. Otherwise, the variable count just evaluates to 0;


A workaround I can suggest is to add a listener to the Dropdown. It should look something like:

var count;
var doc_v1 = document.querySelector('[id=*cbParamVirtual1]')

doc_v1.onchange = function() {
 count = this.length;


Verified that this works on Single Record Update DP, Details DP, and Details drilldown too. Just be mindful on how you reference your elements. (Because, Caspio Elements Naming Convention)

Might work on Inline Insert/Edit too but would need a few additions/modifications to the code.


Hope this helps.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...