This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Jason Sparks
Posted Sep 17 - Read on Facebook

I’m creating a number of layers via a loop. How do I add an onClick event to each layer so clicking on the layer will print the layer name. The following code creates the layers as expected, but when I click on any layer (slot) it outputs the name of the last slot added. Any suggestions?

for i in [0..moviesCount]

slot = new Layer
parent: moviesContent
name: moviesData[i].name
y: slotYVaule
backgroundColor: "transparent"
width: movieContent.width
height: slotTemplate.height
clip: true

slot.onClick ->
print "slotName is " + slot.name

5 Comments

Gary Penner

i think the on.click has to be within the for loop ...

Stephen Crowley

yeah, in the for loop do i.onClick

Jason Sparks

I worked it out. Instead of using the event shortcut of layer.onClick ->, if I use the long hand like this, it works. slot.on Events.Click, (event, layer) ->
print "this slot is " + layer.name

Stephen Crowley

Cool, here is the other way: https://framer.cloud/BLhie

Jason Sparks

Thanks folks, Your suggestions also work, and is shorter than mine. Cheers

Read the entire post on Facebook