0

I need to retrieve some datas from a value field containing a PHP variable. If I try to use JavaScript to output the value on the console I get an "Uncaught TypeError: document.querySelector(...) is null".

$locationMap = get_field('map_location');

<div class="coordinates">
     <input type="hidden" id="lat" value="<?php echo $locationMap['lat']; ?>">
     <input type="hidden" id="lng" value="<?php echo $locationMap['lng']; ?>">
</div>

<script>
     let lat = document.querySelector('#lat').value;
     let lng = document.querySelector('#lng').value;
     console.log(lat);
<script>

If I replace the PHP variable inside the value field with something else (i.e. ABCDF) it work perfectly.

John Shot
  • 296
  • 3
  • 11
  • If you really have that code in the order shown (and assuming you have `` rather than ` – T.J. Crowder Jul 22 '20 at 17:51
  • Hi T.J. Crowder, the scrpt in the code il located in an external js file loaded at the end of the php page (WordPress), and as I said if I replace the PHP variables it works normally. Sorry I forgot to add "/" in the closing script tag. – John Shot Jul 22 '20 at 19:13
  • Ugh, then it'll be some Wordpress thing. The only reason `querySelector` returns `null` is if there's no match for the selector. Your selectors are valid and would match those elements if they were in the page when you called `querySelector`, so that's what you have to debug -- why aren't they? :-) – T.J. Crowder Jul 23 '20 at 07:19

0 Answers0