How to do dynamic "inline" javascript?

I try to use the JQVMap plugin for a custom page template. This only works if I put the javascript into _main.js. But, I want to loop over a custom post type to fill all the countries with a color. Then I need to do something like this, within the custom template:



Please ignore the syntax. I’m migrating this website from another language to Wordpress.

As far as I know, I can’t put this into a separate .js file and load it with the wp_enqueue_script() function as I need to loop over WP_Query results. But “inline” javascript seems not to work either.


Check your console. If it’s not working it will be an issue with the order you’re loading things or the firing timing (your snippet doesn’t wait for document.ready).

You can also look into using wp_localize_script to pass PHP variables to javascript.

Doh! Thanks @Foxaii.