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

JavaScript coding for activating a checkmark box

Community Beginner ,
Mar 22, 2020 Mar 22, 2020

Good day, I've been scratching my head for a while now into finding a JavaScript code for this project of mine. I have a PDF form that I created with very interactive components within. I have a lot of buttons which carry extra information for the users. However, I'm trying to create a JavaScript to put at the beginning of the document entry that would automatically simulate a checkmark into an hidden box (I named it English) because that box will trigger a series of hide and show for other buttons, but I want that checkmark button to be automatically activate at entry without people knowing (the reason why I hide the box to be triggered). I've tried every which way to do this and the most I get is a checkmark in the box without the box actually doing the hide/show function it was made for. Does anybody know the exact coding I should use for this?

Here's what I got so far, and that does the trick to put the checkmark in the existing box named English, but it doesn't make the checkmark box do its mouse up function I assigned to it which is to hide multiple buttons (I got 46).

this.getField("English").checkThisBox(0,event.target.value = "On");

3.6K
Translate
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
LEGEND ,
Mar 22, 2020 Mar 22, 2020

I've move your post to the Acrobat forum for better responses.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

To compare values use ==, not only =

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Thank you for the answer, but with one = or two it does the exact same thing in the results, which is tha it only creates a checkmark in the box and the mouse up event doesn't happen (hiding buttons).  What script would make this happen to tell the button to run it's mouse up event just like if someone would of used it's mouse to select the box but it's only javascript that does it instead?

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

I don't understand.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

What you're trying to do will not work. Setting the value of a check-box with a script does not trigger that field's MouseUp event, which is what I assume you're using to show/hide the other fields. You need to do it yourself in the code that changes the field's value.

The correct way to do that, by the way, is either like this:

this.getField("English").checkThisBox(0, true);

Or:

this.getField("English").value = "Yes";

(or whatever else is the export value of the check-box)

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Thank you for the information and patience with this, I'm very new at using JavaScript and can seriously I don't know much.  Here's a more direct question then.  Is is possible to use a JavaScript that I'd put in the opening of a PDF document, and that script would trigger a command that would make a specific checkmark box run it's action that I would of already tell it to do in the action box (which is to hide 46 buttons)?  If so, what would be such code?  Here's a preview of the action box in the property of the checkmark box I created with Adobe and that I want to have it run using the script.  can this be done or not?  I really appreciate all your help with this.

 
 

English.jpgexpand image

 
 
Translate
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 ,
Mar 22, 2020 Mar 22, 2020

You can show/hide fields with Javascript.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Yes and I did manage to do that before, but in this case it's not just a few fields/buttons, it's involving the manipulation of 96 buttons all together, I don't want to resort into writting a script 96 times to do this, that's why I thought it would be easier to just have a script simulating the box was pushed and then have the box run it's mouse up action by doing that.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

No, not directly. In order to do it you need to change the action associated with the button to JavaScript, place it in a doc-level function and then call it from both the field's MouseUp event and from the doc-level itself.

For example:

 

// doc-level script
function showHideEnglishFields() {
	this.getField("Field1").display = display.visible;
	this.getField("Field2").display = display.visible;
	this.getField("Field3").display = display.visible;
	this.getField("Field4").display = display.hidden;
	this.getField("Field5").display = display.hidden;
	this.getField("Field6").display = display.hidden;
}
showHideEnglishFields();

// MouseUp event of the check-box field
showHideEnglishFields();

 

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Thank you for this informaiton try67, as I just mentioned to Bernd, I did that before for just a few fields, but we're talking 96 buttons to play with in this case, I just thought there might be a way to do it otherwise to avoid having such a long script since I'd have a menu option of English or French at the beginning of the document, and if they press English then it hides half the information buttons and display the other, and if they press French, then it's the opposite.  Which means doing it this way I'd have to write 96 entry in the script twice.  Isn't there an easier way to do it?

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

What names for the buttons does you use?

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

I have 96 different names, they're all associated to the name given to the fields related to them.  As example, here's a fiew:

ContractInfoEnglish

SubContractInfoEnglish

RevisionInfoEnglish

ClassificationInfoEnglish

 

And the same name but with French at the end to differenciate which ones have English text and which ones have English ones.

ContractInfoFrench

SubContractInfoFrench

RevisionInfoFrench

ClassificationInfoFrench

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

You can use a loop over all fields and check the end of the field name.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

You don't need to write it twice. You can add a condition to the script that checks the value of the check-box and based on that hides some fields and shows others. Anyway, you have no other option. That is the only way to do it.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

I was really hoping to avoid doing that, but I had a feeling it would be impossible to trigger a box to run its mouse up event automatically since nobody is actually pressing box.

So if I understand this properly, here's what my script would look like (including the menu I create as well):

var cRtn =  app.popUpMenu("English","Français");

if(cRtn == "English") {

    var ocgs = this.getOCGs();

    for (var i = 0; i < ocgs.length; i++) {

        if (ocgs[i].name == "English") {

            ocgs[i].state = true;

        }

        if (ocgs[i].name == "French") {

            ocgs[i].state = false;

        }

    }

    app.alert("IMPORTANT INFORMATION \n" + "\This is a test.",3);

// Show/Hide buttons for English press

this.getField("ContractInfoEnglish").display = display.visible;

this.getField("SubContractInfoEnglish").display = display.visible;

this.getField("RevisionInfoEnglish").display = display.visible;

this.getField("RevisionInfoFrench").display = display.hidden;

this.getField("RevisionInfoFrench").display = display.hidden;

this.getField("RevisionInfoFrench").display = display.hidden;

}

else if (cRtn == "Français") {

    var ocgs = this.getOCGs();

    for (var i = 0; i < ocgs.length; i++) {

        if (ocgs[i].name == "English") {

            ocgs[i].state = false;

        }

        if (ocgs[i].name == "French") {

            ocgs[i].state = true;

        }

    }

app.alert("INFORMATION IMPORTANTE \n" + "\ Ceci est un test."

,3);

// Show/Hide buttons for French press

this.getField("ContractInfoEnglish").display = display.hidden;

this.getField("SubContractInfoEnglish").display = display.hidden;

this.getField("RevisionInfoEnglish").display = display.hidden;

this.getField("RevisionInfoFrench").display = display.visible;

this.getField("RevisionInfoFrench").display = display.visible;

this.getField("RevisionInfoFrench").display = display.visible;

}

 

And I'd have to write that for the entire 96 different buttons.

 

In a last question, you'll see that I'm using the app.popUpMenu function, which does work, but it creates such a small menu box that it gets lost on the screen lol  Is there a way to make that popUpMenu bigger?  To show the English/French option quite big?

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Correct, for a mouse up event you must use the mouse or on a touch screen the finger.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Ok, thank you for the clarification I'll stop looking for that.  What about the popUpMenu box, can we make that bigger to be more visual?

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

> Is there a way to make that popUpMenu bigger?

No.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

Your code can be greatly shortened and improved if you use something like this:

 

var langs = ["English", "French"];
var baseNames = ["ContractInfo", "SubContractInfo", "RevisionInfo"];

var selectedLang = app.popUpMenuEx({cName: "English", cReturn: "English"}, {cName: "Français", cReturn: "French"});
for (var i in baseNames) {
	for (var j in langs) {
		var f = this.getField(baseNames[i]+langs[j]);
		f.display = (langs[j]==selectedLang) ? display.visible : display.hidden;
	}
}
Translate
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 ,
Mar 22, 2020 Mar 22, 2020

My replies are again not getting picked up...

 

Your code can be greatly shortened and improved if you use something like this:

 

var langs = ["English", "French"];
var baseNames = ["ContractInfo", "SubContractInfo", "RevisionInfo"];

var selectedLang = app.popUpMenuEx({cName: "English", cReturn: "English"}, {cName: "Français", cReturn: "French"});
for (var i in baseNames) {
	for (var j in langs) {
		var f = this.getField(baseNames[i]+langs[j]);
		f.display = (langs[j]==selectedLang) ? display.visible : display.hidden;
	}
}
Translate
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 ,
Mar 23, 2020 Mar 23, 2020

I tried the shorter coding you displayed and I think maybe I'm not putting it properly or something.  Just by itself it seems to work to switch from one display to the other.  However, when I add it to the rest of my script, it doesn't seem to like it, and maybe it's because I'm not putting it at the correct position or I'm duplicating some coding?  Here's what my original coding is:

var cRtn =  app.popUpMenu("English","Français");

if(cRtn == "English") {

    var ocgs = this.getOCGs();

    for (var i = 0; i < ocgs.length; i++) {

        if (ocgs[i].name == "English") {

            ocgs[i].state = true;

        }

        if (ocgs[i].name == "French") {

            ocgs[i].state = false;

        }

    }

    app.alert("IMPORTANT INFORMATION \n" + "\This is a test.",3);

}

else if (cRtn == "Français") {

    var ocgs = this.getOCGs();

    for (var i = 0; i < ocgs.length; i++) {

        if (ocgs[i].name == "English") {

            ocgs[i].state = false;

        }

        if (ocgs[i].name == "French") {

            ocgs[i].state = true;

        }

    }

app.alert("INFORMATION IMPORTANTE \n" + "\ Ceci est un test."

,3);

}

 

And here's what I added at the total end right after, so please tell me what I'm doing wrong:

var langs = ["English", "French"];
var baseNames = ["LanguageInfo", "ContractInfo", "SubContractInfo", "RevisionInfo", "ClassificationInfo", "GeneralInfo", "ProcuringInfo", "SupplyInfo", "OriginatByGov", "OriginatByPrivate", "Branch2", "SubName", "SubAddress", "BriefDescription", "CGP", "MTD", "TypeAccess", "InfoType", "CanInfo", "NatoInfo", "ForeignInfo", "Restrictions", "LevelInfo", "COMSECInfo", "PersonInfo", "SiteAccessInfo2", "SiteAccessInfo1", "EnhancedInfo1", "EnhancedInfo2", "ConfidentialInfo", "SecGuideInfo", "EscortInfo", "StoreInfo", "ComsecStorInfo", "ProductionInfo", "ITInfo", "ITLinkInfo", "Cloud", "StoreInfo", "Signatureinfo", "ProjectAuthoInfo", "GovSecAutInfo", "OrgSecAutInfo", "ITInfo", "ProcOffInfo", "ComsecSecAuthInfo", "ContSecAuthInfo"];

var selectedLang = app.popUpMenuEx({cName: "English", cReturn: "English"}, {cName: "Français", cReturn: "French"});
for (var i in baseNames) {
for (var j in langs) {
var f = this.getField(baseNames[i]+langs[j]);
f.display = (langs[j]==selectedLang) ? display.visible : display.hidden;
}
}

Translate
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 ,
Mar 24, 2020 Mar 24, 2020

What happens when you use it?

Translate
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 ,
Mar 24, 2020 Mar 24, 2020

It's incoherent, it seems to work once and not the second time around, it doesn't remove the second choices on the script if you go back and chose French in example and vice versa.  Where would be the best place to insert that last coding in my script?  Would it be at the total end or total beginning?  And, I'm sort of thinking maybe I'm duplicating some of the functions since in my script I'm already doing an app.popupMenu function, does that make sense?  I like this shorten code and it does work just by itself without anything else, but it doesn't cover my need to call uppon layers that I made and is being called when selecting English or French buttons.

Translate
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 ,
Mar 22, 2020 Mar 22, 2020

I just noticed that another reply I posted here is missing...

 

My reply was:

> Is there a way to make that popUpMenu bigger?

No.

Translate
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