'Troubleshooting a Javascript class' click-event function

I have a JS 'shuffle' function that I am trying to execute every time a user clicks on the shuffleBtn class in my HTML. I am using classes over IDs because I have multiple shuffleBtn instances.

I'm not too familiar with JS/jQuery and can't see why my syntax is broken. Any pointers appreciated!

<script>

window.onload = function() {
  $('.shuffleBtn').on('click', function(){
  var refs = $(".ref");
    for(var i = 0; i < refs.length; i++){
        var target = Math.floor(Math.random() * refs.length -1) + 1;
        var target2 = Math.floor(Math.random() * refs.length -1) +1;
        refs.eq(target).before(refs.eq(target2));
    }
 };
 };
 
</script>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source