How to get selected hyperlink text and save in variable

Welcome to the Streamlit community, and thanks for your detailed question! :blush: You’re running into a common limitation: when you use st.markdown to render HTML <a href=...> links, Streamlit can’t natively detect which link was clicked, because clicking a hyperlink doesn’t trigger a Python callback or update Streamlit’s session state. This is why st.session_state.textpara always ends up with the last value assigned in your loop, regardless of which link is clicked.

With Streamlit’s current features, there is no built-in way to capture which hyperlink was clicked using only st.write or st.markdown with HTML links—this is a browser action, not a Streamlit widget event. To capture user selection in Streamlit, you need to use interactive widgets (like buttons or selectboxes), but you specifically requested not to use buttons. This limitation is confirmed in the Streamlit documentation and community discussions.

If you need to capture which link was clicked, you would need to use a workaround, such as encoding the selection in the URL’s query parameters and reading them back with st.experimental_get_query_params(), but this approach will still cause a page reload. There is no way to update a Python variable in Streamlit from a plain HTML hyperlink click without a page refresh or using a widget.

Sources: