Programmatically jump to anchor on same page after clicking button

Hi @Nico yes, I can open a request on Github np.

By the way, I found a solution that works for now, and it’s pretty simple. By merging solution for other issues, I manage to change slightly the javascript code in https://discuss.streamlit.io/t/jump-to-section-of-components-html/34180/4, defining a scroll_to function, as follows

def scroll_to(element_id):
    components.html(f'''
        <script>
            var element = window.parent.document.getElementById("{element_id}");
            element.scrollIntoView({{behavior: 'smooth'}});
        </script>
    '''.encode())

The important change consists in calling getElementById on the window.parent.document, and not directly on document. I’m not super expert in Streamlit, and it took my a while to realize that components.html creates a new iframe (afaiu), and calling just document.getElementById would miss any element defined in the iframe parent container html. Of course window.parent.document.getElementById solves the issue, and works like a charm.