I would like to pass some PHP into my jQuery append functions. I know I can't pass PHP directly, so I was trying to make the PHP into a variable and then call the variable within the jQuery instead. Is this possible?
Here's what I have so far(not working):
function appendElements() {
var template_dir = "<?php bloginfo('stylesheet_directory'); ?>";
if (window.matchMedia("(min-width: 1100px)").matches) {
jQuery("#container").append("\
<div id='BG' class='bg_images' data-stellar-ratio='.5'>\
<img src='"+template_dir+"/assets/scroll/site/bg_desktop.png'/>\
</div>\
");
}
Add <script> var template_dir = "<?php bloginfo('stylesheet_directory'); ?>"; </script>
in between your <head></head>
tags.
Then run your function as you normally would:
function appendElements() {
if (window.matchMedia("(min-width: 1100px)").matches) {
jQuery("#container").append("\
<div id='BG' class='bg_images' data-stellar-ratio='.5'>\
<img src='"+template_dir+"/assets/scroll/site/bg_desktop.png'/>\
</div>\
");
}
}