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

What is Framer? Join the Community
Return to index
Tyrale Bloomfield
Posted Jul 08 - Read on Facebook

Trying to target a layer with a specific id. I have no idea what the right syntax should be.

Card.id == "1".style = {
"backgroundColor": "#ff0"
}

13 Comments

Jordan Robert Dobson

You have to search for it in a four loop. How many cards are we talking about here?

Chris Camargo

You mean a Element ID, like you'd use for CSS? If you can, I would avoid that method. What I'd do:

1. If your layers belong to a superLayer, give each layer a name, and use mySuperLayer.subLayersByName("myLayerName")[0].backgroundColor = "#FF0"

2. If you're going to be interacting with the layer directly, and this backgroundColor change happens on click, you can do something like this:

myLayer.on Events.Click, ->
@backgroundColor = "#FF0" # Indent this line!

3. You could also just place all of your like-layers in an array called "cards" and do what Jordan is suggesting above.

So my question to you is, which of these sounds easiest to accomplish for your situation?

Joshua Tucker

Tyrale Bloomfield Using what Jordan Robert Dobson suggested, you would have to check the id.

(Assuming cards is an array of the 3 cards you mentioned)

for card in cards
if card.id == "1"
do something

Now, if you were wanting to target a layer by its ID when the layer is not in an array, you can do a for loop through Framer's layer list

for layer in Framer.CurrentContext._layerList
if layer.id == "1"
do something

Jordan Robert Dobson

Sorry I decided to take a nap. Thanks for the backup Joshua and Chris!

Jordan Robert Dobson

Yep just look at each one and find the one you want.

for card in [card1, card2, card3]
card.style = backgroundColor : "#ff0" if card.id is 1

My question is why don't you know which one you want to change?

Jordan Robert Dobson

^ there is an even shorter way to do this. I think it's this:

(card for card in [card1, card2, card3] when card.id is 1)[0].style = backgroundColor : "ff0"

But that's me nerding out on coffeescript.

Jordan Robert Dobson

The problem is you need to put the click handler inside the for loop as well. Otherwise only the last layer set to Card will work.

Jordan Robert Dobson

You might consider having an array store your cards so you can access them easily outside your for loop...

cards = []

for index in [1..3]
cards[index] = card = new Layer ...

Benjamin Den Boer

For new people scanning the thread, one simple way to go about this is by using loops & conditional statements:

Jordan Robert Dobson

Yep! Exactly he was doing some stuff in the loop I didn't quite understand. Wasn't sure if a setup like that would help or not.

Jordan Robert Dobson

Cool. There's a few ways to go about it here. Pick the one that feels right to you!

Aaron James

Benjamin you screenshot above helped me out a ton. Thanks for sharing it!

Jordan Robert Dobson

you could also do

for layer in allLayers when layer.id is 2
layer.x = 300

http://jrdn.io/2Q3m3h2H3042

Read the entire post on Facebook