I have a page where images are loaded automatically. These images are included in the item <a>. I need to find and remove all elements <a>except those that contain the class .private_overlay. I created a very simple piece of code for this:
$('#content a').not($('a').find('div.private_overlay').parent()).remove();
It works when I run it manually. But it does not work with elements that load when the script starts.
I tried to do this:
$(window).scroll(function() {
$('#content a').not($('a').find('div.private_overlay').parent()).remove();
});
It works, in a way. But that is not what I expect. This script should be run with one click.

I just want to apply to work on new elements on this page:

There are many blocks with messages. I am looking for posts flagged as private. To this end, I must hide all untagged messages, including those that have not yet been downloaded.
:

, , , .
:
function searchBox() {
$('body').append('<div id="searchBox"><p style="margin: 0px;"><strong>Wyszukiwarka prywatnych postów:</strong></p> \
<p style="margin: 5px auto; text-align: center"><input id="previousPost" type="button" value="Poprzedni post"></input> \
<input id="nextPost" type="button" value="Następny post"></input></p> \
<p style="margin: 0px;"><input id="hidePosts" type="checkbox"></input><label for="hidePosts" style="vertical-align: bottom;">Kasuj niepotrzebne posty</label></p></div>');
$('#searchBox').css({
position: 'fixed',
left: '20px',
bottom: '20px',
background: 'white',
color: 'rgb(125, 125, 125)',
fontSize: '13px',
border: '1px solid rgb(125, 125, 125)',
borderRadius: '7px',
padding: '7px',
zIndex: '9999',
boxShadow: '0px 0px 4px rgba(0, 0, 0, 0.3)',
display: 'none'
}).fadeIn();
$('#hidePosts').one('click', function() {
$(window).scroll(function(){
$('#content a').not($('a').find('div.private_overlay').parent()).remove();
});
$(this).attr('disabled','disabled');
});
$('#previousPost').click(function() {
$('html, body').stop().animate({
scrollTop: $("#content a").prev().offset().top
}, 2000);
});
$('#nextPost').click(function() {
$('html, body').stop().animate({
scrollTop: $("#content a").next().offset().top
}, 2000);
});
};
, .