I've never done this before so am looking to learn how to do this. I am working in wordpress and creating a plugin. I have a function at the top of my document that adds some jQuery code to the footer of my page, the simplified version of this is as follows:
function slug_masonry_init() { ?>
<script>
jQuery(function($) {
var container = document.querySelector("#skizzar_masonry_loop");
var msnry = new Masonry( container, {
    columnWidth: <?php echo $masonry_item_width ?>,
    gutter: <?php echo $masonry_item_padding ?>,
    itemSelector: ".skizzar_masonry_entry"
});
});
</script>
<?php }
//add to wp_footer
add_action( 'wp_footer', 'slug_masonry_init' );
You will notice within the <script> there are some php variables: $masonry_item_width and $masonry_item_padding
These variables are user defined, the function that handles these is a little further down my document and looks like this:
function skizzar_masonry_shortcode($atts,$content = null) {
    global $skizzar_masonry_item_width, $skizzar_masonry_item_padding;
    $el_class = '';
        extract(shortcode_atts(array(
            "masonry_item_width" => '240',
            "masonry_item_padding" => '5',
            "el_class" => '',
        ),$atts));
        $skizzar_masonry_item_width = $masonry_item_width;
        $skizzar_masonry_item_padding = $masonry_item_padding;
        $output .= '<style>.skizzar_masonry_entry, .skizzar_masonry_entry img {width:'.$masonry_item_width.'px;} .skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:560px; /* + padding */} .skizzar_masonry_entry {margin-bottom:'.$masonry_item_padding.'px; overflow:hidden;}</style>';
        $output .= '<div id="skizzar_masonry_loop" class="'.$el_class.'">';
        $output .= do_shortcode($content);
        $output .= '</div>';
        return $output;
    }
SIDE NOTE: the two global variables you see here: $skizzar_masonry_item_width and $skizzar_masonry_item_width are defined a little further up the document (between the two code snippets) - this is the user interface for inputting width and padding, the code you see here creates a shortcode.
What I don't know how to do is add $masonry_item_padding and masonry_item_width to my jQuery script at the top of the page - currently they are just returning blank.
 
     
    