How to use php code in javascript

What you can do is pass the variable from outside that .js file, like this:

// index.php

<div class="row">
    <div class="small-12 columns">
        page content
    </div>
</div>
<script>var test = '<?php echo $variable; ?>';</script>

and then reference that variable the same as if you defined it within that .js file (mind the potential scope issue).

Leave a Comment