Have any questions?

+44 (20) 3540 6226

hello@appshed.com

Simulate an Item click using JavaScript

0.0/5 rating (0 votes)

You can simulate a user clicking on an Item using JavaScript code.
app.getItem(123).click()

Sometimes you might want to use JavaScript to carry out the same activity that would happen if a user tapped on an Item.

For example, if you have a Sound action on a link... when you click another button, it can play the sound twice, with a delay.
Let's see how you do this.

Firstly, create the Button and the Link.
Add a Sound action to the link and select a Sound file.
Test it out... when you tap the link, it should play the sound.

Now find the Item ID for the link.
Edit the link item, and you will find the ID at the bottom of the list of properties. Copy the ID - we will need it later.

Now go to your Button. We will change the action and use JavaScript.
Write some code to simulate a click on the Link item. The code is:

app.getItem(id).click();

(Replace id with the ID of the link item you copied earlier).

Now when you tap the Button, it will also play the sound.
This is interesting but not especially useful. Here is some code that makes it more interesting:

app.setInterval(function(){
   app.getItem(111111).click();
},2000,5000);

This code creates an interval, which is essentially a loop. You should read up a bit on setInterval, it is an extremely useful JavaScript function.
The interval runs every 2 seconds (2000 ms) and stops after 5 seconds. So it will run twice.

Save and test. You should hear the sound play twice, as per the interval.

The nice thing about the click() function is that we can carry out Actions based on any conditions or other JavaScript code. This gives us the potential of creating very advanced apps.

Try out this basic example, and then use it as your JavaScript skills improve.

Share this product

appshed name white text 250

Copyright © 2017 AppShed Limited