Jump to content

Help opening more id's in script


Claudius

Recommended Posts

The Navigation Bar with Dropdown uses a script to open one dropdown with one id. I need more different dropdowns but don't know how to let getElementById("demo")open more id's. The construction getElementById("demo1", "demo2") doesn't work. Neither does the construction getElementByClass. Please help.

<script>
function myFunction() {
  var x = document.getElementById("demo");
  if (x.className.indexOf("w3-show") == -1) {
    x.className += " w3-show";
  } else { 
    x.className = x.className.replace(" w3-show", "");
  }
}
</script>

Link to comment
Share on other sites

If the element that calls the function is the element with the id ref name use onclick="myFunction(this)" to add argument 'this' (no quotes)

Then change function to

function myFunction(elem) {
  var x = document.getElementById(elem.id);
  if (x.className.indexOf("w3-show") == -1) {
    x.className += " w3-show";
  } else { 
    x.className = x.className.replace(" w3-show", "");
  }
}

 

Edited by dsonesuk
Link to comment
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
×
×
  • Create New...