0

I have an object taken from the DOM who's actual offsetHeight I can not correctly get.

When trying to console.log the object to check the property, it does show the correct offsetHeight. But when I try to get the object.property, then the wrong value shows.

My code (using console.log for simplicity but using the values anywhere in a different way results in the same core problem):

function introBackgroundSize(){
    

    let endpointElement = document.querySelector("div[data-bg--intro='endhere']");
    
    console.log(endpointElement.offsetHeight); //logs 1636(px)
    console.log(endpointElement); //opening to check shows offsetHeight 912(px) (the value I need)
    console.log(endpointElement.offsetHeight); //logs 1636(px)

   
}
introBackgroundSize();

Looking for directions and possible reasons.

Tried using different values and different properties like scrollHeight and swapping around orders. But I keep receiving the wrong value.

What's odd is that it does show the right value in the object, so it's not like the element changes height before or after I try to get the value.

New contributor
foooch is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • .offsetHeight() value is an integer ex. 1636. Maybe what you observe of DOM object when rendered is the true height as a float ex. 1636.5648? When you "check the property" what exactly is "object.property"?
    – zer00ne
    Commented Apr 22 at 20:57
  • You probably did not take the behavior of console.log when logging objects into account. developer.mozilla.org/en-US/docs/Web/API/…: "Information about an object is lazily retrieved. This means that the log message shows the content of an object at the time when it's first viewed, not when it was logged."
    – C3roe
    Commented Apr 23 at 6:34
  • @zer00ne The real height that gets shown on paint is 912, which is the value offsetHeight has when logging the object to the console and checking the value in the array of properties the object has. I don't know if I can share an image, but I can describe it as a dropdown with keys and values, e.g offsetHeight: 912.
    – foooch
    Commented 2 days ago
  • @C3roe Correctly mentioned that I didn't take into account the behavior of console.log when logging objects. I didn't know at all that that was the case. This gave me the idea that a solution would be to call the function after DOMContentLoaded, which then does in fact get the right values. Any idea how an element can have a taller offsetHeight pre DOMContentLoaded? I've read about lower offsetHeight's, because loading elements one by one takes time. It feels like I'm just patching a bandaid and not following proper procedure. But that might just be me...
    – foooch
    Commented 2 days ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.