✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Handle a list of links with one callback

Let’s say I have a list of A-elements on my site that should each execute the same callback when being pressed, while providing their ID so I know which elements has been clicked. The length of this list is dynamic and can potentially contain 100 elements.

My first bet was something like this:

@app.callback(Output('description', 'children'), 
		 [Input('res-'+str(i), 'n_clicks') for i in range(num)])

where num is the number of links in that list. This way I can detect the clicks but I don’t know which element has been clicked and I don’t know how to define the header of the following Python function to accept a dynamic number of parameters.

Another idea is to use the parent element as an Input and create a second callback that would add an .active HTML class to the clicked list element. Would that work?

Any other ideas? Maybe I’m not seeing the obvious solution here :confused:

Thanks,
pietz

A workaround I found was to turn my list of links into radio items and visually removing the circle. My callback gets the radio object as an input from which I can get link ID that was actually clicked. Because of this I’m very limited concerning the style of my list of links, but it works for now.

If someone knows a proper way to handle a list of links with a callback, I would appreciate the help.

Cheers,
pietz