How do you fix this?
By changing the drop() function so that it doesn't move the dragged element if the container is "full". For example, the following pulls the "size" of the container from the data-size attribute and limits the container to that many items:
window.drop = function (ev) {
ev.preventDefault();
var maxLength = Number(ev.target.getAttribute('data-size'));
if (ev.target.children.length >= maxLength) {
return;
}
ev.target.appendChild(
document.getElementById(
ev.dataTransfer.getData('id')
)
);
};
Then you'd simply add a data-size attribute to your container elements. For example:
<div id="inventory" ondrop="drop(event)" ondragover="allowDrop(event)" data-size="1"></div>
<div id="chest" ondrop="drop(event)" ondragover="allowDrop(event)" data-size="10"></div>
The containerToList() function does not work along with the .length function, so I cannot find the size of the Inventory.
Nitpick: The length property, not function (or method in this case).
All three of the ...ToList() functions return arrays, so of course they support the length property, which is an intrinsic property of arrays. For example, both of the following will print the current number of items within the #inventory container:
<<print containerToList('#inventory').length>>
/* OR */
<<print inventoryToList().length>>
I'm unsure why you thought that wouldn't work, but I assure you that it does.