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

Use JavaScript to set onclick event

Explorer ,
Aug 06, 2016 Aug 06, 2016

I'm trying to set an onclick event using JavaScript to a Cp smart shape called "item_01".

When I look at the HTML that is generated the shape does indeed have an onclick event attached but Cp "covers" it with at least two other shape-related elements making it impossible for "item_01" to get clicked by the user.

How can I use JavaScript to attach an onclick event to a smart shape that gets triggered by the user clicking the smart shape?

TOPICS
Advanced , Editing
6.9K
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
People's Champ ,
Aug 08, 2016 Aug 08, 2016

Do you have the smartshape "use as button" option selected?

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
Explorer ,
Aug 08, 2016 Aug 08, 2016

Yes, the smart shape is marked as 'use as button'.

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
People's Champ ,
Aug 09, 2016 Aug 09, 2016

So basically you have a smartshape, used as a button, and you don't have any objects covering it and you cannot click it?

I've found in a few instances, that other elements on the screen can cover other objects, like the div is actually much bigger than the object inside the div.

You can try putting the smartshape at the top of the timeline to see if the stacking order changes.

You can attach event listeners to any object with JS just as you would with a normal HTML page using:

document.getElementById("instancename").addEventListener("click", function(){

   doSomething;

});

But I imagine it still wouldn't be clickable since CP is covering it somehow. I use smartshapes all the time and I've only had issues when it is the last object in the slide array of elements or if I've cloned and object.

You could also use the interactiveItemSubmit listener. e.Data will have the name of the element you clicked.

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
Explorer ,
Aug 09, 2016 Aug 09, 2016

I tried it at the top of the timeline and it made no difference.

Interestingly the event "onmouseover" triggers.

You write "interactiveItemSubmit listener e.Data" like I should know what it is 😉 Can you tell me how to use it or at least find out about 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
People's Champ ,
Aug 09, 2016 Aug 09, 2016

All of the available events available in the Common JS Interface are listed here:

Learn about the Common JavaScript interface for Adobe Captivate

You can add the listener like the example below:

eventEmitterObj.addEventListener( 'CPAPI_INTERACTIVEITEMSUBMIT', interactionFunction, false )

    

function interactionFunction( e )

{       

        console.log( e.Data );     

}

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
Explorer ,
Aug 09, 2016 Aug 09, 2016

Thanks for the info and pointers but still I can't get it to respond to a click. I have this code:

if (window.cpAPIInterface && window.cpAPIEventEmitter) {

  window.cpAPIEventEmitter.addEventListener("CPAPI_SLIDEENTER", initClick, false);

  window.cpAPIEventEmitter.addEventListener('CPAPI_INTERACTIVEITEMSUBMIT', initFunc, false);

}

function initFunc(e) {

  console.log(e.Data);

}

function initClick() {

  console.log("try to set click");

  document.getElementById("item_1").addEventListener("click", function() {

  console.log("item has been clicked");

  }, false);

}

And in the console I get no errors and "try to set click" appearing.

And when I click item_1 the console gives me

Object {itemname: "item_1", frameNumber: 31, objecttype: 612, issuccess: true, slideNumber: 1…}

but "item has been clicked" never appears.

console.log ( document.getElementById("item_1") ) gives

<div id="item_1" class="cp-frameset" tabindex="2500" style="outline-style: none; z-index: 0; -webkit-tap-highlight-color: rgba(0, 0, 0, 0.298039); display: block; left: 191px; top: 85px; width: 137px; height: 63px; visibility: visible; cursor: pointer;"><div id="item_1accStr" class="cp-accessibility"><p>Test</p></div></div>

so I know it can see it but I can't seem to be able to click on it despite e.Data saying that I am.

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
People's Champ ,
Aug 10, 2016 Aug 10, 2016

So the interactive item submit is working and that is what you want.

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
Explorer ,
Aug 10, 2016 Aug 10, 2016

No. While e.Data is showing that "item_1" is being clicked, doing so never triggers

document.getElementById("item_1").addEventListener("click"...

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
People's Champ ,
Aug 10, 2016 Aug 10, 2016

I works for me. I added the eventlistener in the slideenter event.

I don't know where you have the initial code to check the interface but it could be that the element isn't in the DOM yet.

I always use the slideenter event ot attach all event listeners. Even them some elements may not be in the DOM, iFrames seem to lag. I use a timeout for those.

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
Explorer ,
Aug 10, 2016 Aug 10, 2016

Hmm, I have the slideenter event calling initClick which then sets the click event for "item_1". So it should work???

I'll try setting with a timeout.

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
People's Champ ,
Aug 10, 2016 Aug 10, 2016

This is the code is used, all logs were in the console:

var interfaceObj, eventEmitterObj;

window.addEventListener( 'moduleReadyEvent', function ( e )
{
interfaceObj = e.Data;
eventEmitterObj = interfaceObj.getEventEmitter();
initializeEventListeners();
});

function initializeEventListeners()
{
if ( interfaceObj )
{
     if ( eventEmitterObj )
  {
         eventEmitterObj.addEventListener( 'CPAPI_SLIDEENTER', function ( e )
   {   
    window.cpAPIEventEmitter.addEventListener("CPAPI_SLIDEENTER", initClick, false);

      window.cpAPIEventEmitter.addEventListener('CPAPI_INTERACTIVEITEMSUBMIT', initFunc, false);

    function initFunc(e)
    {   
     console.log(e.Data);   
    }

    function initClick()
    {   
     console.log("try to set click");
   
       document.getElementById("item_1").addEventListener("click", function()
       {   
        console.log("item has been clicked");
   
       }, false);
    }   
            });
  }
}
}

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
Explorer ,
Aug 10, 2016 Aug 10, 2016

It still doesn't work!

Is it because I'm entering the code into Cp's javascript window and not as an external file?

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
People's Champ ,
Aug 11, 2016 Aug 11, 2016

I would imagine that's the reason. Your best bet is not to use the JS window except to call functions in an external script file. Everything in the JS Window is converted to a string in the CPM.js. It doesn't always work correctly with all of the escapes. It also doesn't recognize spaces in a functions arguments.

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
Explorer ,
Aug 11, 2016 Aug 11, 2016

I'm now using an external js file and 'click' is still not triggering but when I changed the event to 'mousedown' it did, sort of, trigger.

With a bit more testing I discovered that:

  1. a smart shape that's used as a button does not trigger onclick, even though it shows up in e.Data
  2. a smart shape that's not used as a button does trigger onclick
  3. a smart shape that's used as a button does trigger onmousedown but not on first click???
  4. a smart shape that's not used as a button does trigger onmousedown

It seems weird but I guess that's just a Fact Of Life (FOL) with Cp.

Thanks for all the help. I'm now going to look at tracking pixel coordinates to capture a click event on (rectangular) smart shapes used as 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
People's Champ ,
Aug 12, 2016 Aug 12, 2016

If the interactiveitemsubmit is triggering, the all you need to do is ask what item was clicked and execute your action.

if ( e.Data.itemname == 'item_1' )

{

     doSomething;

}

else

{

return false;

}

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
Explorer ,
Aug 12, 2016 Aug 12, 2016

In my testing on Chrome the problem with that is that if I click on a non interactive element then the interactive element at the bottom of the timelime triggers e.Data irrespective of whether it was clicked or not.

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
People's Champ ,
Aug 15, 2016 Aug 15, 2016

I have noticed that also. You can try putting an clickbox or smartshape used as a button with no action named "dummy" at the bottom of the timeline then check if dummy is clicked.

if ( e.Data.itemname != 'dummy' )

{

     if ( e.Data.itemname == 'item_1' )

     {

          doSomething;

     }

}

else

{

return false;

}

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
Explorer ,
Aug 21, 2016 Aug 21, 2016
LATEST

Should a smart shape at the "bottom" of the timeline trigger an click event when it has not been clicked??? I've no doubt that you're workaround will work but what a silly way for Cp to work. My guess (without working through the JSON) is that Cp is capturing the click on the document and then checking what elements are under the clicked and then if there's no element to accept it erroneously passing it to the last/bottom interactive element.

I'm close to having the tracking of pixel coordinates to capture a click event on (orthogonal aligned and rectangular) smart shapes used as buttons working reliably. I've just got to sort out a layering issue I'm having and then adding a check for whether the element is enabled or not.

Thanks for verifying what I'm seeing.

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
Resources
Help resources