Get a component's id from its event handler

How do I get a component's id from its event handler?

I have a few dozen buttons. When one of the buttons is clicked, I'd like to know what the button's component id is so I can use it in its Click event handler, running JS code. The properties available via self don't seem to include the component id.

A workaround that I'm using is to hide the id as a tooltip and read self.tooltipText from the event handler script, but it would be nice to have a cleaner solution than that. Alternatively, is there a way to associate arbitrary user data on a component?

Update: I don't seem to even be able to refer to a component using self or this from its own properties, other than scripts.

For example, I have a button group and I'd like to compute the labels based on the component's own values, but e.g. {{ this.values }} doesn't work. Is there any way to do this?

Welcome @john_gronska.

Use triggeredById.

https://docs.retool.com/docs/scripting-retool#retrieving-source-of-query-trigger

1 Like

Thanks for your response!

Hmm, triggeredById provides a string reference but there seems to be no way to dynamically look up the control (as in document.getElementById())?

What I'd ideally like to do is be able to set a custom "userInfo" or "context" value on a component (e.g. button1.userInfo = "cancel_emergency"), then be able to look up this value from a query (e.g. var cmdName = triggeredByComponent.userInfo). I can work around the first part with tooltipText and the second part with a component ID to custom value mapping in a temporary state variable, but it would be nicer to do it more "directly".

1 Like

@john_gronska ok so if I am duplicating you correctly you need a pass by reference instead of a string. This is pretty standard for events in other languages. I can see a use case or two for this.

And I think you are also advocating for component custom properties, which I believe are currently immutable. Interesting idea.

Since Retool does not do these now might I suggest putting these into Feature Requests?

Thanks for the feedback. I will put into Feature Requests!

P.S. Doesn't have to be mutable components with arbitrary custom properties, just need one.