DOM.getElementById in GWT doesn't seem to work

I have the following code snippet:

myPanel.getElement().setId("left-content"); //... //... Element e = DOM.getElementById("left-content");// this returns NULL! 

Update

The following is a snippet of code:

 public class RootComposite extends Composite { public RootComposite(int comboSelectedIndex) { VerticalPanel verticalPanel = new VerticalPanel(); initWidget(verticalPanel); VerticalPanel containerPanel = new VerticalPanel(); containerPanel.setSpacing(1); verticalPanel.add(containerPanel); verticalPanel.setSize("100%", "100%"); RightPanelMainComposite rightPanelMainComposite = new RightPanelMainComposite(); VerticalPanel rightcolVerticalPanel = new VerticalPanel(); rightcolVerticalPanel.setStyleName("rightcol"); VerticalPanel searchVerticalPanel = new VerticalPanel(); searchVerticalPanel.setStyleName("search"); rightcolVerticalPanel.add(searchVerticalPanel); searchVerticalPanel.add(rightPanelMainComposite); ContentComposite contentComposite = new ContentComposite(); HorizontalPanel leftContentPanel = new HorizontalPanel(); containerPanel.add(leftContentPanel); leftContentPanel.getElement().setAttribute("id", "left-content"); DOM.getElementById("left-content"); // returns NULL !!! // .... } 

The above composite is added to the RootPanel .

+4
source share
2 answers

You can use DOM.getElementById only if the requested widget is really bound to the DOM. Your code shows that you set the id in the constructor and request the corresponding element immediately after the widget is not connected yet. Your widget is actually bound only when the onLoad method is called.

+10
source

I assume that you did not add myPanel to the DOM (for example, RootPanel.get().add(myPanel) ) before calling DOM.getElementById("left-content") .

+6
source

Source: https://habr.com/ru/post/1338707/


All Articles