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

What is Framer? Join the Community
Return to index
Elliott Samuel Lemberger
Posted Jul 13 - Read on Facebook

Is there a way to get the name of a layer that was clicked on?

12 Comments

Jonas Treub

layerName.name

Elliott Samuel Lemberger

Thanks but wouldn't that require already knowing which layer was clicked on? What if that is what I'm trying to determine? Thanks!

Benny Chew

Maybe you can try something like this: layerA.on Events.Click, (event, layer) ->
print "This layer was clicked", layer.name

Benny Chew

From the docs: layer.on(eventName string, handler function)
Start listening for an event on this layer. For a full list of events see this overview.

When an event is called the first argument is the event information. Depending on the specific event this can contain mouse positions, mouse deltas etc. The second argument is always the layer that the event occurred to.

Elliott Samuel Lemberger

Thanks for this...any idea how I would assign the click event to a layer that is getting created dynamically in a for loop?

Benny Chew

No problem. For my project I did something like this: for i in [1..MAX_TILES]
tileButton = window["Tile"+i]
tileButton.buttonID = i

tileButton.on Events.Click, (event, layer) ->
tileActiveAnim(layer.buttonID)

Benny Chew

In the for loop I just created a var to hold to button, added a unique ID to it. Later I can know which button it is by calling 'layer.buttonID'.

Elliott Samuel Lemberger

Thanks Benny. Could you possibly show me how this code fits in with the new layer code? I can't seem to get the formatting right.

Elliott Samuel Lemberger

Could anyone possibly tell me what I'm doing wrong here? I get undefined when I print buttonID. When I try to print just instance name I get the same one every time no matter what I click. Thanks!

Benny Chew

I believe you have to use the passed variables of the event function, so try print layer.buttonID

Elliott Samuel Lemberger

Hmmm...that also returns undefined. Was this what you meant? Thanks again for your help! Sorry for my lack of coding knowledge!

Benny Chew

You have to declare your (button) id after you have created the layer, otherwise you will overwrite the variable when you created the new layer. Also not sure whether you can define the instance name like this (also needs to be after new layer). Maybe you need to do myVar.name = "instance name".

Read the entire post on Facebook