Most efficient way to have multiple toggle items on a page

Ashley Brandon

If I've got multiple items that I want to change from a display of 'none', to a display of 'block', what's the most efficient way of doing it?

The JS I would use for a single item is below, but I imagine there are several on a page or site. Should I make use of function constructors somehow?

var sideNav = document.getElementById('sideNav');
var menuButton = document.getElementById('menuButton');

function toggle() {
    if(sideNav.style.display) {
        sideNav.style.display = '';
    } else {
        sideNav.style.display = 'block';
    }
}

menuButton.addEventListener('click', toggle);
Calvin Nunes

Take a look, see if this helps you. I did it with vanilla JS, I don't know if you are currently using jQuery (would be easier if yes)

What I did:
Every button have it's own id that is used to "connect" to the elements that it should toggle.

  1. First I add the listener to all buttons, passing it's id when the function is called.
  2. Then in the function, I used document.querySelectorAll to get all elements with the class that should be hidden/show.
  3. Then finally I run a loop in those elements, showing or not showing, depending on it's current 'display'.

var menuButtons = document.querySelectorAll('.menuButton');
menuButtons.forEach(function(btn){
  btn.addEventListener("click", toggle.bind(this, btn.id));
})

function toggle(id) {
    var sideNav = document.querySelectorAll('.nav_' + id);
    sideNav.forEach(function(el){
      if (el.style.display == 'none'){
        el.style.display = "block";
      } else {
        el.style.display = "none"
      }
    })
}
div{
  height: 30px;
  width: 50px;
  margin: 2px 0;
  background: #999;
  text-align: center
}
<button id="menuButton1" class="menuButton">Toggle 1</button>
<button id="menuButton2" class="menuButton">Toggle 2</button>
<button id="menuButton3" class="menuButton">Toggle 3</button>

<div class="nav_menuButton1">1</div>
<div class="nav_menuButton1">1</div>
<div class="nav_menuButton2">2</div>
<div class="nav_menuButton3">3</div>
<div class="nav_menuButton3">3</div>
<div class="nav_menuButton3">3</div>

Probably there are better approaches, but I'm now in a hurry and this is the best I could think in that moment

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Most efficient way to check for existence of multiple items

From Dev

Most efficient way to remove multiple items from a IList<T>

From Dev

What is the most efficient way to toggle expand/contract split windows in vim?

From Dev

Most efficient way to script toggle buttons on and off functionality

From Dev

Most efficient way to pass multiple arguments to a function?

From Dev

Most efficient way to sort by multiple fields in Java

From Dev

Deleting multiple files in most efficient way (ansible)

From Dev

Most efficient way to recode these multiple if statements

From Dev

Most Efficient Way to Replace Multiple Characters in a String

From Dev

Most efficient way to recode these multiple if statements

From Dev

Most efficient way to pass multiple arguments to a function?

From Dev

Most efficient way to query a database multiple times

From Dev

Most efficient way for handling multiple files

From Dev

Most efficient way to loop finding items not in a list in python

From Dev

What is the most efficient way to change the UI of the items in a RecyclerView?

From Dev

Most efficient way to change all items in a sublist to a string?

From Dev

Most efficient/fastest way to delete/remove items of a vector

From Dev

What is the most efficient way to hide element completely from your page?

From Dev

What's the most efficient way to visit a .html page?

From Dev

What is the most efficient way to hide element completely from your page?

From Dev

What is the most efficient way to Navigate to a XAML page? wp8

From Dev

Most efficient way to implement this?

From Dev

Most efficient way to execute this

From Dev

What is the most efficient way to query multiple collections in MongoDB?

From Dev

Most efficient way to process multiple callbacks into one receiver function

From Dev

Most efficient way to count occurrences in XQuery for multiple values

From Dev

What is the most efficient way to do multiple or comparisons if some are conditional

From Dev

What is the most efficient way to display a table with multiple columns in iOS?

From Dev

Most efficient way to validate multiple textboxes against a tolerance value

Related Related

  1. 1

    Most efficient way to check for existence of multiple items

  2. 2

    Most efficient way to remove multiple items from a IList<T>

  3. 3

    What is the most efficient way to toggle expand/contract split windows in vim?

  4. 4

    Most efficient way to script toggle buttons on and off functionality

  5. 5

    Most efficient way to pass multiple arguments to a function?

  6. 6

    Most efficient way to sort by multiple fields in Java

  7. 7

    Deleting multiple files in most efficient way (ansible)

  8. 8

    Most efficient way to recode these multiple if statements

  9. 9

    Most Efficient Way to Replace Multiple Characters in a String

  10. 10

    Most efficient way to recode these multiple if statements

  11. 11

    Most efficient way to pass multiple arguments to a function?

  12. 12

    Most efficient way to query a database multiple times

  13. 13

    Most efficient way for handling multiple files

  14. 14

    Most efficient way to loop finding items not in a list in python

  15. 15

    What is the most efficient way to change the UI of the items in a RecyclerView?

  16. 16

    Most efficient way to change all items in a sublist to a string?

  17. 17

    Most efficient/fastest way to delete/remove items of a vector

  18. 18

    What is the most efficient way to hide element completely from your page?

  19. 19

    What's the most efficient way to visit a .html page?

  20. 20

    What is the most efficient way to hide element completely from your page?

  21. 21

    What is the most efficient way to Navigate to a XAML page? wp8

  22. 22

    Most efficient way to implement this?

  23. 23

    Most efficient way to execute this

  24. 24

    What is the most efficient way to query multiple collections in MongoDB?

  25. 25

    Most efficient way to process multiple callbacks into one receiver function

  26. 26

    Most efficient way to count occurrences in XQuery for multiple values

  27. 27

    What is the most efficient way to do multiple or comparisons if some are conditional

  28. 28

    What is the most efficient way to display a table with multiple columns in iOS?

  29. 29

    Most efficient way to validate multiple textboxes against a tolerance value

HotTag

Archive