I'm using the following code to control a div with the ID 'sidebar'
var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('#sidebar').addClass('fixed');
} else {
// otherwise remove it
$('#sidebar').removeClass('fixed');
}
});
However when a page that odes not contain div#sidebar it throws out an error stating #sidebar is null (because it's not there!)
How can I convert this code to only happen IF there is div#sidebar on the page?