views:

32

answers:

3

I'm not getting any effect out of DOM.setElementAttribute, am I doing something wrong?

class MyListBox extends com.google.gwt.user.client.ui.ListBox {
  ....
  protected void setHoverAutoWidth() {
    addDomHandler(new MouseOverHandler() {
      public void onMouseOver(MouseOverEvent event) {
        DOM.setElementAttribute(getElement(), "width", "auto");
      }
    }, MouseOverEvent.getType());
    addDomHandler(new BlurHandler(){
      public void onBlur(BlurEvent event) {
        DOM.setElementAttribute(getElement(), "width", "100px");
      }
    }, BlurEvent.getType());
  }
}

(I know there are less hacky ways to change the width than to set the style attribute directly, but I don't care about css right now.)

Edit: Oops, just realized that width does not change the style width, just adds a width attribute to the tag (which explains why nothing happens). Any suggestions on how to modify the style are still welcome!

+1  A: 

I'm pretty sure that will attempt to set the width attribute of the element (which may not exist for the type of element you are working on), NOT the CSS width property.

You're probably looking for setStyleAttribute.

Hank Gay
+1  A: 

Use DOM.setStyleAttribute

Tahir Akhtar
+1  A: 

In later versions of GWT you can use getElement().getStyle().setWidth(100, Unit.PX), which gives you a bit of extra compile-time correctness over the String-based methods in the DOM class. Use getElement().getStyle().clearWidth() to set the width back to auto.

hambend