0

Hello everybody I'm new in javascript. I want to create a button which duplicate elements in an array. I try this :

   %a.btn.btn-success#addd{ :href => "#", :onclick => "duplicate(this.parentNode.parentNode)" }

       :javascript
       document.getElementById('addd').click = duplicate;

       var i = 0;
       var original = document.getElementById('inf_finding.id');

       function duplicate(original) {
         var clone = original.cloneNode(true);
         clone.id = "inf_finding.id" + ++i;
         original.parentNode.appendChild(clone);
       }

But I refresh it, the element that I add is not in the page anymore.Can you help me to resolve this please ? Here are the screenshots :

First

It add properly the element

But when I refresh the page, the element disappears.

1
  • 2
    Of course, when you reload the page, everything is reset. What did you expect :) You have to implement some kind of persistence, the easiest being probably the LocalStorage that can store strings and is unaffected by page refreshes. Commented Aug 8, 2018 at 12:59

1 Answer 1

1

Your code only makes changes within the DOM of the current webpage. When the page is refreshed, the DOM is regenerated from the HTML sent by the server and so your changes are lost.

In order to make persistent changes, you need to save data either on the server (using AJAX) or in the browser using Local Storage, Cookies, etc.

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

Comments

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.