if(previous_href == undefined) { previous_href = $('#post_list').children().children().last().find('a').attr('href'); $('#previous_button').fadeOut(); //optional - remove if you want to loop to end } //apply hrefs to next / previous buttons $('#next_button').attr('href', next_href); $('#previous_button').attr('href', previous_href); });