0

I load div with PHP and then I want to get it from HTML using Javascript. Getting element by id alerts undefined.

 <?php echo "<div id='myDiv'>Hello</div>"; ?>
 <script>
     $( document ).ready(function() {
    alert( $("#myDiv").innerHTML)
});
</script>

How is that even possible?

vytaute
  • 1,260
  • 4
  • 16
  • 36
  • Your question doesn't actually make sense: all PHP code is run before any JavaScript code, so there is no delay for the PHP to be "done loading". Presumably, there's actually a problem in the LoadOfficesTable function, but since you haven't shown it to us, we can't tell you what it is. – IMSoP Nov 02 '21 at 17:58
  • Duplicate of [Why is innerHTML returning 'undefined'?](https://stackoverflow.com/questions/29435612/why-is-innerhtml-returning-undefined) `document.getElementById()` or `document.querySelector()` return an [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element), which has a `innerHTML` property. `$("#myDiv")` returns a jQuery object, which doesn't have a `innerHTML` property, hence it is `undefined`. Use `$("#myDiv").html()`. – Ivar Nov 03 '21 at 15:35

0 Answers0