Jump to content

Hide-Show all elements with same ID


jandrews3

Recommended Posts

I'm writing a script which needs to hide all elements of a certain ID on page load, and display them upon clicking the link 'Toggle'. The following code hides all the questions, but only shows the first question when 'Toggle' is clicked even though they all have the same ID. I'm very new at javascript and greatly appreciate your help. Thanks.

 

<script type="text/javascript">
function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="block")
which.style.display="none"
else
which.style.display="block"
}
</script>
 
<a href="javascript:hideshow(document.getElementById('adiv'))">Toggle</a>
 
 
 
<div id="adiv" style="font:24px bold; display: none">Question 1</div>
<div id="adiv" style="font:24px bold; display: none">Question 2</div>
<div id="adiv" style="font:24px bold; display: none">Question 3</div>
<div id="adiv" style="font:24px bold; display: none">Question 4</div>
<div id="adiv" style="font:24px bold; display: none">Question 5</div>
<div id="adiv" style="font:24px bold; display: none">Question 6</div>
<div id="adiv" style="font:24px bold; display: none">Question 7</div>
<div id="adiv" style="font:24px bold; display: none">Question 8</div>
<div id="adiv" style="font:24px bold; display: none">Question 9</div>
<div id="adiv" style="font:24px bold; display: none">Question 10</div>
<div id="adiv" style="font:24px bold; display: none">Question 11</div>
<div id="adiv" style="font:24px bold; display: none">Question 12</div>
 

 

Link to comment
Share on other sites

id's, by definition, must each be unique on the page, i.e. you cannot use the same id more than one time on a page.

 

to do what you want, show/hide a group of elements, you can either put the elements inside a containing <div id="adiv"> or give each element the same class name and show/hide them based on the class name.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.