How do I use jQuery to do and undo a series of actions using two different buttons?

I am using a jQuery to generate some content and it is working fine:

		$( ".compare-list i.fa-share-alt" ).each(function(index) {
	    $(this).on("click", function(){
				$(this).closest('').prepend('<div class="socialShareIconsContainer"><div class="socialShareIcons"><div class="socialShareIcon"><i class="fa fa-facebook"></i></div><div class="socialShareIconClearer"></div><div class="socialShareIcon"><i class="fa fa-twitter"></i></div><div class="socialShareIconClearer"></div><div class="socialShareIcon"><i class="fa fa-envelope"></i></div></div><a class="closeSocialShareIcons">x</a></div>');

Open in new window

What I need to do is, when I click on that a.closeSocialShareIcons which was created on the fly, I would like to remove the div.socialShareIconsContainer that was created on the fly with all its contents

I've tried quite a lot of things but I really give up now :(

thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Because the elements are created on the fly, you'll need to attach their click events to an element that will persist on the page such "<body>".

$("body").on('click', 'a.closeSocialShareIcons', function() {

That, or you attach the click handler to the element when you create it.  So rather than prepending a string, you create each element and attach them to each other.  If the above doesn't work for you, let's explore this option.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.