Search code examples
javagwtgwtquery

Get scrollHeight of a DIV


I am trying to get the scrollHeight of a div with this code:

    GQuery element = $(".pre.line-numbers");
    String height = element.attr("scrollHeight");
    Window.alert(height); // empty!
    $("pre.line-numbers")
            .css("overflow-y", "hidden")
            .css("overflow-x", "auto")
            .css(CSS.HEIGHT, height + "px"); // here

But everytime, the String height is empty string.

I double checked with the browser inspector and I can see the height is there. Furthermore, to verify this, I manually set the height to a specific value, and I can see that is applied to the pre.line-numbers div

What am I missing here? What is the correct way to get the scrollHeight for all major browsers(like Firefox and Chrome)?


Solution

  • This is the code to get the "scrollHeight" with GwtQuery:

    int height = $("pre.line-numbers").get(0).getPropertyInt("scrollHeight");
    $("pre.line-numbers").css("overflow-y", "hidden")
                         .css("overflow-x", "auto")
                         .css(CSS.HEIGHT, height + "px");