• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using script to count dropdowns... two scripts

Community Beginner ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

I have two sets of dropdowns... one set is numbered D1 - D64 with all of the selections the same. The other set is E1 - E64 all with the same selections but not the same as set D.

I have a script for each set that will calculate how many times a selection was made and it places the total in a text field automatically.

The problem is I place both scripts as document level scripts. When I do this, I can only get one to function. If I place either script in by itself, it functions. Is there a way to combine these scripts or some way to make both work at the same time?

function countDropdowns(v) { 

    var total = 0; 

    for (var i=1; i<=64; i++) { 

        var f = this.getField("D"+i); 

        if (f.valueAsString==v) total++; 

    } 

    event.value = total?total:""; 

function countDropdowns(v) { 

    var total = 0; 

    for (var i=1; i<=64; i++) { 

        var f = this.getField("D"+i); 

        if (f.valueAsString==v) total++; 

    } 

    event.value = total?total:""; 

TOPICS
PDF forms

Views

635

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

Are these the actual functions, or did you accidentally copy the same one twice?

You cannot define two functions with the same name. They must have different function names, and they have to be written to operate on different sets of fields. Note that the second copy is still looking at the "D" fields.

Like this:

function countDropdownsD(v) {

    var total = 0;

    for (var i=1; i<=64; i++) {

        var f = this.getField("D"+i);

        if (f.valueAsString==v) total++;

    }

    event.value = total?total:"";

}

function countDropdownsE(v) {

    var total = 0;

    for (var i=1; i<=64; i++) {

        var f = this.getField("E"+i);

        if (f.valueAsString==v) total++;

    }

    event.value = total?total:"";

}

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

My mistake. Thank you for catching that... Here are the correct scripts I have as of now. How do I change them?

function countDropdowns(v) { 

    var total = 0; 

    for (var i=1; i<=64; i++) { 

        var f = this.getField("D"+i); 

        if (f.valueAsString==v) total++; 

    } 

    event.value = total?total:""; 

function countDropdowns(v) { 

    var total = 0; 

    for (var i=1; i<=16; i++) { 

        var f = this.getField("E"+i); 

        if (f.valueAsString==v) total++; 

    } 

    event.value = total?total:""; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

Thom,

I did a copy and paste from your response with the different function names. I still only get one script to work at a time.

Could this have something to do with the calculation from the text field that should be counting them?

This is what I have:

Two Document JavaScripts

The first I named countDropdownsD

function countDropdownsD(v) {

    var total = 0;

    for (var i=1; i<=64; i++) {

        var f = this.getField("D"+i);

        if (f.valueAsString==v) total++;

    }

    event.value = total?total:"";

}

The second is named countDropdownsE

function countDropdownsE(v) {

    var total = 0;

    for (var i=1; i<=64; i++) {

        var f = this.getField("E"+i);

        if (f.valueAsString==v) total++;

    }

    event.value = total?total:"";

}

In the text fields where I want the selections counted, I have this:

countDropdowns("selection name");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2017 Dec 09, 2017

Copy link to clipboard

Copied

LATEST

Since you changed the function names, You also have to change the names where the functions are called.

Details are really important with any kind of programming.

Perhaps you should consider hiring a professional.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

I provided the solution in your original thread. Why are you not implementing it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

I couldn't find that thread. You did provide some great solutions but I added another set of dropdowns with different values contained in them.

You can see above that I've tried a couple of different ways to get both scripts to work at the same time. I can only seem to get one to work at a time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

There's a link to in any email you get, and it's also accessible under your profile.

For future reference: Calcuculating the number of times an item is selected from a dropdown list

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines