events - Moving items between two lists in JavaScript - Stack Overflow

I want to move items between two lists. All list items have buttons within them, once this button is cl

I want to move items between two lists. All list items have buttons within them, once this button is clicked, item should be moved to the other list. I have created JavaScript event but it works only one way - so the item can be moved once e.g. from list1 to list2 but when I try to click on the button again, it doesn't work. Can you look at me code and advise how can I assign my event also to newly created items (these that just have been moved)?

 document.addEventListener("DOMContentLoaded", function () {

    var buttons = document.querySelectorAll(".moveBtn");

    var list1 = document.getElementById("list1");

    var list2 = document.getElementById("list2");

        function moveItem(e) {
        var newItem = document.createElement("li");

        if (this.parentElement.parentElement.id === "list1") {
            list2.appendChild(newItem);


        } else {
            list1.appendChild(newItem);

        }

        newItem.innerHTML = this.parentElement.innerHTML;
        this.parentElement.parentNode.removeChild(this.parentElement);

    }

    for (var i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener("click", moveItem);
    }

 })

I want to move items between two lists. All list items have buttons within them, once this button is clicked, item should be moved to the other list. I have created JavaScript event but it works only one way - so the item can be moved once e.g. from list1 to list2 but when I try to click on the button again, it doesn't work. Can you look at me code and advise how can I assign my event also to newly created items (these that just have been moved)?

 document.addEventListener("DOMContentLoaded", function () {

    var buttons = document.querySelectorAll(".moveBtn");

    var list1 = document.getElementById("list1");

    var list2 = document.getElementById("list2");

        function moveItem(e) {
        var newItem = document.createElement("li");

        if (this.parentElement.parentElement.id === "list1") {
            list2.appendChild(newItem);


        } else {
            list1.appendChild(newItem);

        }

        newItem.innerHTML = this.parentElement.innerHTML;
        this.parentElement.parentNode.removeChild(this.parentElement);

    }

    for (var i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener("click", moveItem);
    }

 })
Share Improve this question asked Oct 27, 2016 at 16:44 JoannaJoanna 2891 gold badge7 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Rather than destroying the old element and creating a new one, just move the element:

function moveItem(e) {
    var moveTo = this.parentElement.parentElement.id == "list1" ? list2 : list1;
    moveTo.appendChild(this.parentElement);
}

Note also that there's no need to match on id; you can match on the actual element:

function moveItem(e) {
    var moveTo = this.parentElement.parentElement == list1 ? list2 : list1;
    moveTo.appendChild(this.parentElement);
}

Live Example:

document.addEventListener("DOMContentLoaded", function() {
    var buttons = document.querySelectorAll(".moveBtn");
    var list1 = document.getElementById("list1");
    var list2 = document.getElementById("list2");

    function moveItem(e) {
        var moveTo = this.parentElement.parentElement == list1 ? list2 : list1;
        moveTo.appendChild(this.parentElement);
    }

    for (var i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener("click", moveItem);
    }
});
#list1 {
  border: 1px solid green;
}
#list2 {
  border: 1px solid blue;
}
<div id="list1">
  <div>
    Item 1
    <button class="moveBtn">Move</button>
  </div>
  <div>
    Item 2
    <button class="moveBtn">Move</button>
  </div>
  <div>
    Item 3
    <button class="moveBtn">Move</button>
  </div>
</div>
<div id="list2">
  <div>
    Item 4
    <button class="moveBtn">Move</button>
  </div>
  <div>
    Item 5
    <button class="moveBtn">Move</button>
  </div>
  <div>
    Item 6
    <button class="moveBtn">Move</button>
  </div>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600784a4635388.html

相关推荐

  • events - Moving items between two lists in JavaScript - Stack Overflow

    I want to move items between two lists. All list items have buttons within them, once this button is cl

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信