JavaScript I am moving elements around depending on screen width using jQuery, but it is creating duplicates moving elements around javascript,jquery moving elements around

This is not something I'm actually going to use, I know I should not have two headers on a page, this is just an example of my problem.

This works perfectly when there is only one on the page, but when there are two it constantly duplicates the list items. What is causing this? Am I using the (window).resize function incorrectly?

https://jsfiddle.net/ozmay3um/

<header>
    <nav class="navbar" role="navigation">
        <ul class="nav">
            <li><a target='_blank' href="#">Page One</a></li>
            <li><a target='_blank' href="#">Page Two</a>
                <ul class="subpages">
                    <li class="subpage">Subpage One</li>
                    <li class="subpage">Subpage Two</li>
                    <li class="subpage">Subpage Three</li>
                </ul>
            </li>
            <li><a target='_blank' href="#">Page Three</a></li>
        </ul>      
    </nav> 
</header>

$(document).ready(function() {  

  if ($(window).width() <= 991) {
    // if on load window is smaller than 992px, add subpages to main nav
    $('.subpage').appendTo('.nav');
  }

  $(window).resize(function() {
    if ($(window).width() >= 992) {
      // if window is larger than 992px, add subpages to sub nav
     $('.subpage').appendTo('.subpages');

    } else {
      // otherwise add to main nav
      $('.subpage').appendTo('.nav');

    }
  });

});
Answer:1

Your code is not operating on each header individually so it actually moves items from one header to another (showing duplicates). You can change your code to only operate on each header i individually.

You could do this instead:

$(document).ready(function() {  

  $(window).resize(function() {
    if ($(window).width() <= 992) {
      // if window is small than 992px, add subpages to sub nav
        // otherwise add to main nav
        $('.subpage').each(function() {
            $(this).closest(".nav").find(".subpages").append(this);
        });

    } else {
        // otherwise add to main nav
        $('.subpage').each(function() {
            $(this).closest(".nav").append(this);
        });
    }
  }).resize();

});

Working demo: https://jsfiddle.net/jfriend00/s7fpqfys/

This code finds the parent .nav item and then finds the right place to append the subpages within that particular .nav hierarchy. This keeps the reorganization local to the particular .nav hierarchy and won't move items between headers.

P.S. I also think you had your >= backwards. I assume you want to use the subnav only when the window is narrow, not when it's wide.

And, I removed the duplicate code that runs initially by instead just triggering the first resize() event to force the initial layout.

Answer:2

I'm trying to execute a piece of code within an empty isolated execution context in JavaScript. In the below sample, I'm trying isolate isolated execution scope. What I want to do is to execute a ...

I'm trying to execute a piece of code within an empty isolated execution context in JavaScript. In the below sample, I'm trying isolate isolated execution scope. What I want to do is to execute a ...

  1. isolated execution context

How can I access the binary representation with JavaScript from a file uploaded with a file input?: <input type="file" name="file"> I can access the details of the uploaded file successfully ...

How can I access the binary representation with JavaScript from a file uploaded with a file input?: <input type="file" name="file"> I can access the details of the uploaded file successfully ...

  1. file input value html
  2. file input value jquery
  3. file input value javascript
  4. file input value react
  5. file input value
  6. file input value fakepath
  7. file input value php
  8. file input value clear
  9. file input value display
  10. input file value set
  11. input file value reset
  12. input file value default
  13. input file value change
  14. input file value base64
  15. input file value laravel
  16. input file value change event
  17. input file value url
  18. input file value empty
  19. input file value get
  20. input file value angular

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

  1. javascript some method
  2. javascript array some method
  3. javascript object some method