1

EDIT: I just noticed that I'm trying to swap integers because I'm getting the rowIndex. That's not what I want to do. I want to swap objects. However, I don't really know how.

I want to move table elements up and down in a queue by swapping with the element above and the element below respectively. However, my code doesn't work. The following is the JS function for the downButton.

var downButton = document.getElementsByClassName('downButton')[0];
downButton.onclick = function moveDown(currentRow) {
  var index = currentRow.parentNode.parentNode.rowIndex;
  var nextRow = currentRow.nextElementSibling.parentNode.parentNode.rowIndex;
  var temp = index;
  index = nextRow;
  nextRow = temp;
}

This is the html for the down button:

<button class = "downButton">down</button>

Here's a JSFiddle link for the table: https://jsfiddle.net/Led4b3nw/

2

1 Answer 1

0

Swap innerHTML instead of indexes:

  var index = currentRow.parentNode.parentNode;
  var nextRow = currentRow.nextElementSibling.parentNode.parentNode;
  var temp = index.innerHTML;
  index.innerHTML = nextRow.innerHTML;
  nextRow.innerHTML = temp;
Sign up to request clarification or add additional context in comments.

2 Comments

It gives me the following error: read property 'parentNode' of undefined at HTMLButtonElement.moveDown, I'm assuming because of removing rowIndex property.
This cannot be debugged without your full code then. It seems your currentRow is undefined. Did you forget to pass the value to the function?

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.