Jump to content

JS basic issue

wowpeter

Hi,

I have a problem. Basicly let's have 10 normal html headers under each other and a button that will swap them(The 1st one will be now the last one and so on). I'd probably do it using fro loop.

czech boi

Link to comment
Share on other sites

Link to post
Share on other sites

It might be "logic" to you what you want, but to mess up someones code is pretty hard.
The info you gave me, just do a reverse for loop, the first will be the last etc.
 

let i = array.length,
array = [xy];

for(;i == 0;i-- ){
  console.log(array[i]);
}

If you want a better answer, start posting your code here. If you're woried about someone stealing your stuff, I have bigger projects on my github where people can download it..

AMD FX8320 | GTX660 | 4x4GB DDR3 | LG ZR2740W

Logitech Wireless Pro  | Logitech G413 | Nuforce uDAC5  | AKG K612

Link to comment
Share on other sites

Link to post
Share on other sites

Im assuming you wanted to swap two html elements in the DOM...
 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <style>
      h3 {
        margin-top: 0px;
        margin-bottom: 0px;
      }
    </style>
    <title>Swapping Two HTML Elements</title>
  </head>
  <body>
    <h3>0</h3>
    <h3>1</h3>
    <h3>2</h3>
    <h3>3</h3>
    <h3>4</h3>
    <h3>5</h3>
    <h3>6</h3>
    <h3>7</h3>
    <h3>8</h3>
    <h3>9</h3>
    <button id="button">Click</button>
    <script>
      // https://linustechtips.com/main/topic/1037144-js-basic-issue/
      // https://stackoverflow.com/questions/9732624/how-to-swap-dom-child-nodes-in-javascript
      button.addEventListener("click", function() {
        let headers = document.querySelectorAll("h3");
        headers[9].parentNode.insertBefore(headers[9], headers[0]);
      });
    </script>
  </body>
</html>

https://codesandbox.io/s/myqz0w1mqp

If this isnt what you wanted then please edit the codesandbox to reflect you problem so we can help you better.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×