0

Is it possible to move an array element with jquery? so if i click on it, it would append in the array aswell and not just visually on the dom?

var cardsInHand = ["1", "2", "3", "4", "5", "6", "7"];
var cardsOnField = [];

const cardsInHandEl = document.querySelector("#cardsInHand-el");

for (var i = 0; i < cardsInHand.length; i++) {
    cardsInHandEl.innerHTML +=
        `
    <button>
        ${cardsInHand[i]} 
    </button>
    `
}

$(document).on("click", "#cardsInHand-el button", function () {
    $("#onField-el button")
        .remove()
    $(this)
        .appendTo($("#onField-el"));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
    <ul id="cardsInHand-el">Cards in Hand: </ul>
    <ul id="onField-el"></ul>
    <script src="script.js"></script>
</body>

4
  • 1
    jQuery was built as a UI add-on. What you are trying to do is outside that scope Commented Aug 3, 2021 at 15:37
  • See array.splice Commented Aug 3, 2021 at 15:38
  • You can move dom elements in the dom, and you can move array items in an array. you cannot move dom elements in the dom by moving items in an array. Commented Aug 3, 2021 at 15:38
  • Short answer - no. There is no 'back and forth' between elements within the DOM and the contents of JS arrays. If you wanted to implement something like this, you should use a UI framework such as VueJS vuejs.org Commented Aug 3, 2021 at 15:41

2 Answers 2

1

You can use Array#push to push the item to the array and Array#splice to remove in the click event listener.

var cardsInHand = ["1", "2", "3", "4", "5", "6", "7"];
var cardsOnField = [];

const cardsInHandEl = document.querySelector("#cardsInHand-el");

for (var i = 0; i < cardsInHand.length; i++) {
  cardsInHandEl.innerHTML +=
    `
    <button>
        ${cardsInHand[i]} 
    </button>
    `
}

$(document).on("click", "#cardsInHand-el button", function() {
  $("#onField-el button")
    .remove()
  $(this)
    .appendTo($("#onField-el"));
  const number = $(this).text().trim();
  cardsOnField.push(number);
  cardsInHand.splice(cardsInHand.indexOf(number), 1);
  /* debugging purposes only */
  console.clear();
  console.log('Cards in hand: ' + cardsInHand);
  console.log('Cards on field: ' + cardsOnField);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <ul id="cardsInHand-el">Cards in Hand: </ul>
  <ul id="onField-el"></ul>
  <script src="script.js"></script>
</body>

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

Comments

1

That's more or less how UI libraries would do it:

var cardsInHand = ["1", "2", "3", "4", "5", "6", "7"];
var cardsOnField = [];

const cardsInHandEl = document.querySelector("#cardsInHand-el");
const cardsOnFieldEl = document.querySelector("#onField-el");

function render(arr, el) {
    el.innerHTML = '';
    for (var i = 0; i < arr.length; i++) {
        el.innerHTML +=
            `
            <button>
                ${arr[i]} 
            </button>
            `
    }
}
render(cardsInHand, cardsInHandEl);
render(cardsOnField, cardsOnFieldEl);

$(document).on("click", "#cardsInHand-el button", function () {
    const number = $(this).text().trim();
    cardsOnField.push(number);
    cardsInHand.splice(cardsInHand.indexOf(number), 1);
    render(cardsInHand, cardsInHandEl);
    render(cardsOnField, cardsOnFieldEl);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
    <ul id="cardsInHand-el">Cards in Hand: </ul>
    <ul id="onField-el"></ul>
    <script src="script.js"></script>
</body>

2 Comments

what would be the benefit of an UI librarie? o:
That you don't have to implement the code yourself. The way I did that is considered cleaner nowadays on one hand but on the other hand it has some overhead by re-rendering the whole list (which isn't a problem if the list is as small as yours)

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.