0

I'm using HTML slim for the code below.

.page
  .paper
    button.button.js-copier I copy things

- content_for :js_includes do
  javascript:
    var startingHtml = $('.page').html();

    function initializePlugin() {
      $('.js-copier').each(function () {
        $(this).on('click', function () {
          $('.page').append(startingHtml);
          $(document).trigger('page-refreshed');
        });
      });
    }

    // Run it right away
    initializePlugin();

    $(document).on('page-refreshed', initializePlugin);

Is there a way to fix the fact that when I click on the "I copy things" button, it creates multiple copies of the button as it is iterating over all the button from the button that was selected?

Also, is there a better way to write this code. All I'm trying to do is duplicate the button anytime I click on any buttons(first and any new button created buttons)

-Anthony

3
  • 1
    what are you trying to achieve? Commented Feb 1, 2018 at 16:53
  • 1
    Read jQuery's doc on Understanding Event Delegation particularly Event Propagation section. I feel whatever you are trying to do can be simplified once you understand this. Commented Feb 1, 2018 at 16:54
  • @messerbill Just want to duplicate a button after clicking on it. Right now it is duplicating the button, but it is duplicating multiple times. Commented Feb 1, 2018 at 17:22

1 Answer 1

2

First change this:

  $('.js-copier').each(function () {
    $(this).on('click', function () {
      ...

to this:

  $('.page').on('click', '.js-copier', function () {
    ...

Read this to understand https://learn.jquery.com/events/event-delegation/#event-propagation

Then remove the 'page-refreshed' event because you don't need it:

   $(document).trigger('page-refreshed');
   ...
   $(document).on('page-refreshed', initializePlugin);

Demo solution:

var startingHtml = $('.page').html();

function initializePlugin() {
  $('.page').on('click', '.js-copier', function () {
    $('.page').append(startingHtml);
  });
}

// Run it right away
initializePlugin();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="page">
  <div class="paper">
    <button class="button js-copier"> I copy things</button>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, does this mean that I don't need the 'page-refreshed' event?
I don't know ... if you are just using it to attach the events, then yes. Notice that if your script is before you elements you will need to wait for the load event in order to attach events. It don't seem to be your case though.
The trigger method that was removed was triggering this event. So, i guess we don't need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.