• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

show hide all divs with one click

So here's the deal.  I have a typical web form,  at the top of the form is a link called "Show Help", if the user clicks on this link then a little image shows up next to each and every form field, the image is  surrounded by a link that will send a user to the help page.  The images are stored in their own divs but I don't want to show each one-at-a-time, I want to either turn them all on or all off.

The code below works but since the divs all have the same id, and since I'm using GetElementById, the code only works for the first item on the form.  I tried GetElementByName and GetElementByClass and neither of them work.

Any thoughts would be appreciated.
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
function toggle(linkid){
 var linkval = document.getElementById(linkid).innerHTML
 if(linkval=='Show Help'){
       document.getElementById(linkid).innerHTML = 'Hide Help';
       document.getElementById('HelpIcon').style.display='inline';
 }
 if(linkval=='Hide Help'){
       document.getElementById(linkid).innerHTML = 'Show Help';
       document.getElementById('HelpIcon').style.display='none';
 }
}
</script>
</HEAD>
 
<BODY>
<a href="#" id="TheHelpLink" onclick="javascript: toggle(this.id)">Show Help</a>
<form>
Form Field One: <input name="ff1" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
<br>
Form Field Two: <input name="ff2" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
</form>
</BODY>
 
</HTML>

Open in new window

0
xamian
Asked:
xamian
  • 6
  • 3
  • 2
1 Solution
 
MMDeveloperCommented:
try this

divs = document.getElementsByTagName("DIV");

for (x in divs) {
      el = divs[x];
      if (el.id == "HelpIcon") {
            el.style.display = "none";
      } else {}
}



and to show, just run the code again and set the style.display to "inline".
0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
One comment to MMDeveloper code - if you use any external libaries  if you use statemenet   (var x in something) it might be broken, because new created elements by those libaries using Array.prototype addes to aray like they are a normal objects... Oh well, just a little update to MMDeveloper code:

divs = document.getElementsByTagName("DIV");

for (var x=0,y=divs.length;x<y;x++) {
      var el = divs[x];
      if (el.id == "HelpIcon") {
            el.style.display = "none";
      } else {}
}


I add some "var" there to ensure that temporary variables wont become global variables. Here are some quote:

"Declaring Variables
You can declare a variable in two ways:

With the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables.
By simply assigning it a value. For example, x = 42. This always declares a global variable and generates a strict JavaScript warning. You shouldn't use this variant."

read more: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Variables
0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
I read the first sentence that i wrote and i did not understand it ^^. Here are some example. You can extend every object in Javascript, even those that are normally implemented in javascript like Array

Array got for example method push(element)

var a=[1,2,3,4,5];
a.push(6);

But what if i want more functions in this array? this is simple: just type

Array.prototype.someNewFunction = function (parameter)
{
   alert(parameter);
}


And if you do this every Array instance in javascript will have this method, so now you can do:

a.someNewFunction("i like prototype");

But there is one problem. All methods inserted like that are counted as an elements in (var x in y) statement (but only in this one). So... I advice you (this is mine experience of changing alot of code to (var x in y)) just never use this sentence, unless you are 100% sure that you wont use any other libaries, like prototype, maybe some facebook API etc. I hope that now its a little more clean uff:)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Pawel WitkowskiSenior Javascript DeveloperCommented:
"(this is mine experience of changing alot of code to (var x in y))" of course i mean (this is mine experience of changing alot of code from (var x in y) to (var x=0,y=length;x<y;x++)
0
 
MMDeveloperCommented:
you know wilg32, I've always wondered why sometimes when I do that, I get JS errors and when I alert the current array item, it shows function code and whatnot. I never put 2+2 together to think that it was an external javascript library causing it (prototype/scriptaculous). I ended up giving in and using their Array.each() methods.
0
 
xamianAuthor Commented:
So I tried MMDeveloper's code but that didn't work. I probably implemented it incorrectly.  I don't really get what the rest of you guys are talking about, I just want to show and hide a couple of divs all at once.

Any thoughts?
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
function toggle(linkid){
divs = document.getElementsByTagName("DIV");
 
for (x in divs) {
      el = divs[x];
      if (el.id == "HelpIcon") {
            el.style.display = "inline";
      } else {}
}}
</script>
</HEAD>
 
<BODY>
<a href="#" id="TheHelpLink" onClick="javascript: toggle(this.id)">Show Help</a>
<form>
Form Field One: <input name="ff1" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
<br>
Form Field Two: <input name="ff2" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
</form>
</BODY>
</HTML>

Open in new window

0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:

<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript"><script language="javascript">
function toggle(){
divs = document.getElementsByTagName("DIV");
 
for (x=0,y=divs.length;x<y;x++) {
      var el = divs[x];
      if (el.getAttribute("id") == "HelpIcon") {
            el.style.display = (el.style.display=="none")?"block":"none";
      }
}}
</script>
</HEAD>
 
<BODY>
<a href="#" id="TheHelpLink" onClick="toggle()">Show Help</a>
<form>
Form Field One: <input name="ff1" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
<br>
Form Field Two: <input name="ff2" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
</form>
</BODY>
</HTML>

Open in new window

0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
try this one ;)
0
 
xamianAuthor Commented:
Sorry wilq32 it didn't work, at least in IE 7. Keep getting object expected error.
0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
I did not noticed that i pasted <script> twice.. here check this one
<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
function toggle(){
var divs = document.getElementsByTagName("div");
 
for (x=0,y=divs.length;x<y;x++) {
      var el = divs[x];
      if (el.getAttribute("id") == "HelpIcon") {
            el.style.display = (el.style.display=="none")?"block":"none";
      }
}}
</script>
</HEAD>
 
<BODY>
<a href="#" id="TheHelpLink" onClick="toggle()">Show Help</a>
<form>
Form Field One: <input name="ff1" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
<br>
Form Field Two: <input name="ff2" type="text">
<div id="HelpIcon" style="display:none">
<a href="help.htm?TopicId=1"><img src="help.gif" width="533" height="70" /></a>
</div>
</form>
</BODY>
</HTML>

Open in new window

0
 
xamianAuthor Commented:
Awesome solution, I never would have thought about that, thanks a lot.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now