0

How should solve the following problem?

The div content changes but height does not change after updating innerHTML by XMLHttpRequest.

This is the main file:

<html>
<head>
<script>
    function loadpage() 
    {   
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() 
        {
            if (this.readyState == 4 && this.status == 200) 
            {
                document.getElementById("mydiv").innerHTML = this.responseText;
            }
        }

        xhttp.open("GET", "page.html", true);
        xhttp.send();

        document.title = document.getElementById("mydiv").clientHeight;
    }

</script>
</head>

<body>
    <div id="mydiv" onclick="loadpage()">hello</div>
</body>
</html>

This is the file page.html

<html>
<body>
1<br>
2<br>
3<br>
4<br>
5<br>
6<br>
7<br>
8<br>
9<br>
10<br>
</body>
</html>

2 Answers2

0

After using ".innerHTML" you may have to force-reset the height like this:

document.getElementById("mydiv").style.height = ""
Josh Wortman
  • 99
  • 1
  • 4
0

Try to set the fit-content value to height property on "mydiv".

#mydiv {
height: fit-content;
}