user6043723 user6043723 - 3 months ago 8
Javascript Question

jQuery - Call a div class instead of a div id

I'm still learning javascript and jQuery.

<script src="macy.js"></script>
<script>
Macy.init({
container: '#macy-container',
trueOrder: false,
waitForImages: false,
margin: 24,
columns: 2,
breakAt: {
1200: 5,
940: 3,
520: 2,
400: 1
}
});
</script>


Instead of a div id called
macy-container
. I would like to call a class called
macy-container
.

This is what I tried:

<script src="macy.js"></script>
<script>
Macy.init({
container: '.macy-container',
trueOrder: false,
waitForImages: false,
margin: 24,
columns: 2,
breakAt: {
1200: 5,
940: 3,
520: 2,
400: 1
}
});
</script>


It didn't work. I tried looking for a solution but other solutions didn't work.

Answer

I would try wrapping the init in a document ready function. The page may not be done rendering and trying to find this class that hasn't been built yet.

<script>
$( document ).ready(function() {
    Macy.init({
    container: '.macy-container',
    trueOrder: false,
    waitForImages: false,
    margin: 24,
    columns: 2,
    breakAt: {
        1200: 5,
        940: 3,
        520: 2,
        400: 1
    }
    });
});
</script>