Cataclysm
Cataclysm

Reputation: 8588

Vaadin : How to get Element by ID?

How to get HTML Elmement (or DOM) in Vaadin ?
In GWT I can use as DOM.getElementById("myId"); I can set id attribute on my Vaadin components by setId() method. For example:

    Button button = new Button("Say Hello");
    button.setId("myButton");

So, how can I retrieve this DOM Element in Vaadin ?

Upvotes: 4

Views: 11694

Answers (3)

jlemon
jlemon

Reputation: 105

In Vaadin8 you may try this:

JavaScript.getCurrent().execute("document.getElementById('refreshButton').click()");

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 339362

Vaadin 10 (Vaadin Flow)

The new Vaadin Flow generation replaces the internal use of GWT for Web Components.

This new architecture provides us with easy direct access to the DOM from the Java-based server-side, if you so desire. You can read the DOM, and you can manipulate elements in the DOM. Read about the new Element API in the manual.

Vaadin 6, 7, & 8 (Vaadin Framework)

This Answer expands on the comment by Vaadin expert, Henri Kerola.

Vaadin is a server-side app framework. It's purpose is to shield the app developer from the details of HTML, CSS, JavaScript, DOM, GWT, HTTP, WebSocket, and such web technologies. The app developer writes in pure Java (and maybe a tiny touch of CSS for tweaking). Vaadin transparently and auto-magically generates the HTML-CSS-JavaScript-GWT-DOM necessary to render a representation of the app’s user-interface within a web browser.

So there is no way to access the DOM from that Java server-side, nor any need to do so generally.

If you want to take control of the web technologies then Vaadin is probably not the best framework for you.

Upvotes: 1

Topera
Topera

Reputation: 12389

You can use this:

public static Component findComponentById(HasComponents root, String id) {
    for (Component child : root) {
        if (id.equals(child.getId())) {
            return child; // found it!
        } else if (child instanceof HasComponents) { // recursively go through all children that themselves have children
            Component result = findComponentById((HasComponents) child, id);
            if (result != null) {
                return result;
            }
        }
    }
    return null; // none was found
}

Source: https://vaadin.com/forum/#!/thread/3199995/3199994

Upvotes: 8

Related Questions