Skip to main content
Participating Frequently
September 22, 2021
Answered

Buttons change and Javascript

  • September 22, 2021
  • 2 replies
  • 4688 views

Hello Everyone,

 

First of all, i know nothing about Javascript but here is what i lke to see on my fIllable pdf.

 

By clicking on the same button few times, i would like to see it changing by an other button and so on.

 

Button 1: a square

Button 2: a circle

Button 3: blank

 

So, when i click one time, it goes from a square to circle next by clicking again, to circle to blank and by cliking again, blank to sqaure .... etc..

 

Thank you very much

 

This topic has been closed for replies.
Correct answer Nesa Nurani

I have a button with a D12 yellow dice image. His caption is Proficiency.

I have a second button with a D8 green dice image. His caption is Ability.

 

On the button where i need to click (button 0), it is blank, empty at the beginning.

Each time we click on, i'd like to see it changing from empty to "Ability" to "Proficiency" to empty to ...etc..


Don't forget to set button to 'icon only' in button -> properties -> options tab, then use this script as Mouse UP:

var e = this.getField("Empty").buttonGetIcon()
var a = this.getField("Special 1").buttonGetIcon();
var p = this.getField("Special 2").buttonGetIcon();
if(event.target.buttonGetIcon() == e)
event.target.buttonSetIcon(a);
else if(event.target.buttonGetIcon() == a)
event.target.buttonSetIcon(p);
else if(event.target.buttonGetIcon() == p)
event.target.buttonSetIcon(e);

2 replies

Thom Parker
Community Expert
Community Expert
September 24, 2021

It sounds like you want a 3 state button. This can be done with a script, and much easier than having 3 separate buttons.  Use the Adobe Pi font  to make the caption font into a shape.

 

The this code will work for every button in the same way.  Add it to a Mouse Up "Run a JavaScript" action

switch(event.target.buttonGetCaption())
{
   case " ": 
      event.target.buttonSetCaption("n");
      break;
   case "n": 
      event.target.buttonSetCaption("l");
      break;
   default: 
      event.target.buttonSetCaption(" ");
      break;
}

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
September 24, 2021

Thank you Thom for your reply.

 

At the beginning i created 3 separate buttons because my square and my circle are more like icon coming from image (png), not font.

 

Would it be possible to do the same effect with button changing on click ?

 

Thom Parker
Community Expert
Community Expert
September 27, 2021

Yes, the circle and square icons can be saved as images in a hidden button, then acquired and applied with the "field.buttonGetIcon" and "filed.buttonSetIcon" functions. You'd just change the button properties to only display an image, but keep the script the same and then add the code to set the icons into it.  However, unless your icons are more than a plain square and circle, you are better off using fonts.  

 

You'll fine a sample PDF that uses this icon swapping technique here:

https://www.pdfscripting.com/public/Free-Sample-PDF-Files-with-scripts.cfm

 

Look for the "Swat the Fly" game.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
September 23, 2021

Do you mean you want to change the text of a single button each time it's clicked, or do you want to have multiple buttons, each one with a different caption (or image) and show/hide them in order?

Participating Frequently
September 23, 2021

Each one has a different caption. There is no text.

I 've found a solution by making 3 different buttons (a square, a circle and a blank) and next, i set them up by show/hide when i click on them. All this 3 buttons are on the same little area like 3 buttons in one.

 

The thing is that i need to create 150 little areas with 3buttons each !

If i create copies, i thought it could work but the copies keep the setting of the initial button so each time i click on a new copy, all the copies and the orginal change.

 

That 's why, i guess, the only way is to input a script.

I hope to be clear enought.

Thank you

Bernd Alheit
Community Expert
Community Expert
September 23, 2021

You must use unique names for the buttons