Skip to content
Advertisement

JavaScript insertAfter and insertBefore

I am playing around with the JavaScript functions insertAfter and insertBefore, however I am trying to insertAfter and insertBefore two elements.

For instance, consider the following HTML:

JavaScript

Then I have this JavaScript code snippet:

JavaScript

Basically when the Up class is called, the previous row is moved up and when the Down class is called, the current row is moved down one row.

What I want to do, is move the rows Up/Down 2 rows… meaning something like row.prev().prev() or row.next().next() however this does not seem to work.

Is there an easy way around this?

Would appreciate any help/suggestions.

Advertisement

Answer

to go up

JavaScript

to go down

JavaScript

obviously the tr must exist prev/next have to exist

NB you are caching row as the first tr element so row is changing every time the first tr element change

listen to event

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement