Edit on
<script id="item-1" type="text/x-handlebars-template">  
  Item 1
</script>  
<script id="item-2" type="text/x-handlebars-template">  
  Item 2
</script>  
<script id="item-3" type="text/x-handlebars-template">  
  Item 3
</script>  
<script id="item-4" type="text/x-handlebars-template">  
  Item 4
</script>  
<script id="item-5" type="text/x-handlebars-template">  
  Item 5
</script> 
<script id="item-6" type="text/x-handlebars-template">  
  Item 6
</script> 
<script id="item-7" type="text/x-handlebars-template">  
  Item 7
</script> 
<script id="item-8" type="text/x-handlebars-template">  
  Item 8
</script>

<h1>Moving content around with Javascript templates</h1>
<p>A codepen demo to go along with the blogpost: <br /><a href="http://blog.colouringcode.com/moving-content-around-with-javascript-templates/">blog.colouringcode.com/moving-content-around-with-javascript-templates</a></p>
<div id="content">  
    <div class="content content-primary">
        <div class="item item-1"></div>
        <div class="item item-2"></div>
        <div class="item item-3"></div>
        <div class="item item-4"></div>
        <div class="item item-5"></div>
    </div>
    <div class="content content-secondary">
        <div class="item item-6"></div>
        <div class="item item-7"></div>
        <div class="item item-8"></div>
    </div>
</div>  
$body: #eee;

* {
  margin: 0;
  padding: 0;
  
  text-align: center;
}

body {
  background: $body;
  
  padding: {
    top: 80px;
  }
  
  text-align: center;
  line-height: 1.4;
}

h1 {
  font: {
    family: arial;
    size: 36px;
    weight: normal;
  }
  margin: {
    bottom: 20px;
  }
}

p {
  max-width: 800px;
  
  font: {
    family: arial;
    size: 16px;
    weight: normal;
  }
  
  color: #666;
  
  margin: 0 auto 80px;
  
  a {
    color: #555;
    text-decoration: none;
  }
}

#content {
  max-width: 960px;
  margin: 0 auto;
  
  @media (min-width: 600px) {
    .content {
      &-primary {
        float: left;
        width: 60%;
      }
      &-secondary {
        float: right;
        width: 35%;
      }
    }     
  }
}

.item {
  background: #333;
  margin: 0 auto 20px;
  padding: 20px 0;
  
  max-width: 800px;
  
  color: #fff;
  font: {
    family: arial;
    size: 16px;
    weight: normal;
  }
  
  p {
    
    margin: {
      bottom: 0;
    }
  }
}
View Compiled
var content = [  
  { id: 'item-1', mobile: 2, tablet: 3, desktop: 1 },
  { id: 'item-2', mobile: 4, tablet: 4, desktop: 2 },
  { id: 'item-3', mobile: 5, tablet: 5, desktop: 3 },
  { id: 'item-4', mobile: 6, tablet: 6, desktop: 4 },
  { id: 'item-5', mobile: 7, tablet: 7, desktop: 5 },
  { id: 'item-6', mobile: 1, tablet: 2, desktop: 6 },
  { id: 'item-7', mobile: 3, tablet: 1, desktop: 7 },
  { id: 'item-8', mobile: 8, tablet: 8, desktop: 8 }
  ];

var myFunction = function(theMQ) {
	$('.item', '#content').empty();

  if (theMQ === 'mqCore') {
    // functions for core media query
   $(content).each(function(i, item) {
      $('.item-' + item.mobile, '#content').html($('#' + item.id).html());
    });
  } else if (theMQ === 'mq600') {
    // functions for the 600 media query
    $(content).each(function(i, item) {
      $('.item-' + item.tablet, '#content').html($('#' + item.id).html());
    });
  } else if ((theMQ == 'mq960') || (theMQ == 'mq1200')) {
    // functions for the 960 and 1200 media queries
    $(content).each(function(i, item) {
      $('.item-' + item.desktop, '#content').html($('#' + item.id).html());
    });
  }
  
}

$(function() {
  checkMQ.addFunction(myFunction);
  checkMQ.init();
});

// https://github.com/jonnyhaynes/checkmq
window.checkMQ = (function() {

  var theFunctions = [], theBreakpoints;

  theBreakpoints = [
    { theName: 'mqCore', theQuery: window.matchMedia('screen and (max-width: 599px)') },
    { theName: 'mq600', theQuery: window.matchMedia('screen and (min-width: 600px) and (max-width: 959px)') },
    { theName: 'mq960', theQuery: window.matchMedia('screen and (min-width: 960px) and (max-width: 1199px)') },
    { theName: 'mq1200', theQuery: window.matchMedia('screen and (min-width: 1200px)') }
  ];

  // Check MQ on document ready
  var whichMQ = function() {

    var theMQ;

    for (var i = 0; i < theBreakpoints.length; i++){
      if (theBreakpoints[i].theQuery.matches) {
        if (theMQ !== theBreakpoints[i].theName) {
          theMQ = theBreakpoints[i].theName;
        }
      }
    }

    loadFunctions(theMQ);

    return theMQ;

  };

  // Event listener for changes in MQ
  var changeMQ = function() {

    for (var i = 0; i < theBreakpoints.length; i++){
      theBreakpoints[i].theQuery.addListener(whichMQ);
    }

  };

  // Load the functions
  var loadFunctions = function(theMQ) {

    for (var i = 0; i < theFunctions.length; i++){
      theFunctions[i](theMQ);
    }

  };

  // Add functions to theFunctions array
  var addFunction = function(fn) {

    theFunctions.push(fn);

  };

  // Init
  var init = function() {

    whichMQ();
    changeMQ();
    return this;

  };

  // Provide some public methods for access outside the module
  return {
    init: init,
    whichMQ: whichMQ,
    changeMQ: changeMQ,
    addFunction: addFunction
  };

})();
Rerun