Jason G. Designs

WordPress Themes, Design Tutorials, Linux Themes and more…

Video Tutorial- How to Move an Element Position Before and After with JavaScript

In this tutorial, we will take an unlimited number of similar elements on the page, and move one item ahead of or before the current one.

Video TransCript

A while back, I created jgdGalleryMaker, an online photo gallery service. It allows users to upload photos and rearrange them before copying and pasting them to a website.

This tutorial is not about jgdGalleryMaker, however. It is about replicating the code that ‘rearranges’ the (in this case) image tiles.

Stripping down the concept to it’s bare basics, we want to take an unlimited number of similar elements on the page, and move one item right ahead of or to the left before the currently selected item.

Setting Up

For those who want to follow along as I am explaining, I have set up a Tutorial Resources page on GitHub that has the index file, a CSS file, an empty main.js file and a main-js.txt file you can copy and paste from.

Step 1

If you’ve watched my previous tutorial about selecting a next and previous tile, one of the first things we did was add a counter to the top of the file. We will repeat that here.

This counter initializes to zero, then uses a changeBy function to count up or down.

Two other functions use the changeBy function with a parameter of 1 or -1. They are the countUp and countDown functions.

The value function keeps track of the the counting result.

Step 2

The first thing we need to do is add a variable to reference all of our div elements on the page.

For this, we use document.querySelectorAll to choose all HTML elements with a class of box.

Each function, moveTileRight and moveTileLeft need their own references to the tile variable and declaration, so we will add the first reference to tile at the top of the moveTileRight function.

tile variable and declaration

The next thing we need to reference is our total number of divs with the box class. How do we do this?

Well, the count of the divs, our nodelist, starts from 1 and in this case, since there are 5 elements, it counts up to five.

But since our counter starts at zero, we will subtract 1 from our total number of div elements, assigning this declaration to a listLength variable.

listLength variable that sets the nodelist length minus 1

Let’s also add the tile variable declaration to the top of the moveTileLeft function as well.

tile variable for the moveTileLeft function

In this step, we also want to setup click events for each button.

Click events for the move tile functions

Step 3

Now we can start working on the moveTileRight functionality by first adding the countUp function from our counter.

countUp method from the counter

Then, assign the counter’s value method to a nextIndex variable.

nextIndex variable that holds the counter's current value

This increments the index number up 1 with each click. We can console log the nextIndex variable to see this in action.

Oops, I forgot to add the variables to reference our buttons at the top and the counter. Let’s add those.

Button variables and counter code

Next we check if the tile we are currently on is not undefined.

If it is not, then we insert the current tile after the tile that comes next. This can get tricky, so let me explain.

When we click, the index will go to the next tile up. What we want is to take the current tile, which after clicking, will be in the position one level before the current one, and move it ahead of the current tile.

For this, we use JavaScript’s ChildNode.after function.

Code that inserts current div after the next one

Similarly to how we did in the Select Previous and Next tutorial, we will also add code to disable the next button when we reach the end of our nodelist.

Conditional that disables the next button at the end of the nodelist

This is important, as it stops the index from counting.

Step 4

Next, we can work on the functionality for the moveTileLeft function.

Let’s add the countDown method from our counter.

countDown method from the counter

Our prevIndex variable is set to the count.value method, just like in the moveTileRight function.

Assignment of current index to prevIndex variable

We, again, check if the current index is not undefined, but this time upon clicking, the index will go to the tile before the current one.

Thus, we move the tile that is one above the current one and place it before the current tile.

Code to place the current tile before the previous one

We can also add the code to disable the previous button if the previous index is equal to zero.

Disable previous button when the nodelist is at position 0

One final thing we should do for our Move Left button is to set it to disabled on screen load so users can’t click back at first page load.

To do that, we simply add the disabled attribute to the button. We will put this just below the variables.

Disable previous button on page load

Step 5

Even though these if statements are straight forward, these is a more concise way to type these. We will create ternary statements in their place in both functions.

Here, I pasted these in from main-js.txt. Let’s use the one in moveTileRight, for example.

First, we create a variable to hold the ternary condition.

Then, with everything inside parentheses, we first set the condition the if statement would check for.

That is followed by a question mark that denotes the two things we will be choosing between.

After the question mark is a block of code that gets executed if the condition is true, a colon, and the block of code that gets executed if the condition is false.

moveTileRight ternary statement for next button disabling

And here we have a similar ternary for the moveTileLeft function.

moveTileLeft ternary statement

Putting everything all together, here is our final moving tile project.

Conclusion

To conclude, we learned several things:

  • How to add and interpret a counting function
  • How to integrate that counter into a nodelist, our tile elements
  • How to use the JavaScript after and before functions to move elements
  • How to use a ternary in place of a simple if else statement

Thank you for watching this tutorial. Links to the tutorial resources and key references are in the description.

And if you [watched the video on YouTube], don’t forget to hit the Like button and subscribe. Thank you, my viewers.

Resources


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.