I'm trying to insert a few lines of new code into my site. The code needs to be inserted when the browser width is less than 768px, and if the code already exists, it doesn't need to be inserted again. For some reason whenever I resize the browser to less than 768, the code is inserted twice and then once more every time I resize it. Does anyone know why this line of code is being inserted more than once?
$(window).resize(function() {
if ($(window).width() < 768) {
if ($(document).not('#step-4-circle')) {
$('<div id="step-4-circle" class="step-circle"><div class="inner"><span class="number">4</span><span class="title">FULFILL ORDERS</span><span class="subtitle">where you want</span></div></div>').insertBefore('#how-step-3');
}
}
});
Adjust the conditional to check if any elements were selected:
JS Fiddle: http://jsfiddle.net/3zP8H/1/