Updating contents length / size on click

Updating contents length / size on click

Hi Everyone, new to the forum.

I don't know how to title this as it's not easy to explain.

I have the following code: https://jsfiddle.net/xpvt214o/448284/

You can click the - in the right of the widget to send it to trash.

This works beautifully but I want to be able to offer a way for the user to clear the trash "folder".

I first added the following:

  1. $( '.trashwidgets' ).append( '<a class="cleartrash" href="#">Clear</a>' );

That works but everytime you add a widget to the trash, it duplicates the button, which I don't want. I just want one button to clear it all.

So I tried using the length parameter as follows:

  1. if( $( '.trashwidgets .cleartrash' ).length <= 1 ) {
  2.  $( '.trashwidgets' ).append( '<a class="cleartrash" href="#">Clear</a>' );
  3. }

You can see this in action https://jsfiddle.net/doqtbLxj/

I know I am missing something simple, what's the best way to accomplish this?