Jump to content
Claudius

Help opening more id's in script

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>

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...