Skip to main content
emmal48105553
Participating Frequently
November 29, 2018
Answered

Using Javascript to define button actions

  • November 29, 2018
  • 1 reply
  • 1974 views

HI all,

I have adobe acrobat dc pro. I have a document that I want to add a button that opens an accordion like paragraph below it. Adobe does not give any error codes on the code, which is html, css, and javascript combined. However, when the button is clicked, nothing happens. I created another button on the same page to open google and it works so i know it s not an error in the button function overall. Here is the code I am trying to use. I have tried creating a document javascript, and a action javascript and nothing seems to work. Any help would be greatly appreciated! My overall plan is to try and code into the document an accordion function, since it is not built in.

<script>

<div id="accordion">

     <div class="panel active"> <!-- first panel -->

          <div class="acc-header">header1</div>

          <div class="acc-body">body of panel 1</div>

     </div>

     <div class="panel"> <!-- second panel -->

          <div class="acc-header">header2</div>

          <div class="acc-body">body of panel 2</div>

     </div>

     <div class="panel"> <!-- third panel -->

          <div class="acc-header">header3</div>

          <div class="acc-body">body of panel 3</div>

     </div>

</div>

function initAccordion(accordionElem){

 

  //when panel is clicked, handlePanelClick is called.         

  function handlePanelClick(event){

      showPanel(event.currentTarget);

  }

//Hide currentPanel and show new panel. 

 

  function showPanel(panel){

    //Hide current one. First time it will be null.

     var expandedPanel = accordionElem.querySelector(".active");

     if (expandedPanel){

         expandedPanel.classList.remove("active");

     }

     //Show new one

     panel.classList.add("active");

  }

  var allPanelElems = accordionElem.querySelectorAll(".panel");

  for (var i = 0, len = allPanelElems.length; i < len; i++){

       allPanelElems.addEventListener("click", handlePanelClick);

  }

  //By Default Show first panel

  showPanel(allPanelElems[0])

}

initAccordion(document.getElementById("accordion"));

</script>

This topic has been closed for replies.
Correct answer try67

Hi, so I am not really sure what isn't working here. I have limited coding experience, but I do know a little matlab and in that coding the rect[1] is getting changed because on the first click it would change it to the new rect[1] because the original statement meets the condition. But I don't know javascript so maybe I'm missing something??? If you could explain a little more what you mean that would be awesome!

Here's the code. I changed it a little but as I said, not really sure what to do now.

And thank you for all the help so far!

// Function that moves a button named google down on first click of

// button and back up again on the second click of button

function move()

{

fld = this.getField("google");

var rect = fld.rect;

//calculating the new position

rect[0];

rect[1];

rect[2];

rect[3];

if(rect[1] == 648){ //if equals original position

rect[1] = 648 - 70; //move to new position

}else{ //if doesn't equal original position

rect[1] = 648; //move to original position

}

}


Use this code:

function move() {

    var fld = this.getField("google");

    var rect1 = [0, 0, 0, 0]; // fill in with actual values

    var rect2 = [1, 1, 1, 1]; // fill in with actual values

    var currentRect = fld.rect;

    if (currentRect[0] == rect1[0]) {      //if equals original position

        fld.rect = rect2;      //move to new position

    } else {                    //if doesn't equal original position

        fld.rect = rect1;          //move to original position

    }

}

1 reply

Bernd Alheit
Community Expert
Community Expert
November 29, 2018

This is Javascript for web browsers, not PDF files.

emmal48105553
Participating Frequently
November 29, 2018

Ok, good to know. I did not realize there was a difference.

try67
Community Expert
Community Expert
November 29, 2018

Open the Console window (Ctrl+J) and you'll see plenty of error messages...