Solved

show hide all divs with one click

Posted on 2008-10-28
11
887 Views
Last Modified: 2012-08-13
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
Comment
Question by:xamian
  • 6
  • 3
  • 2
11 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22825131
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
 
LVL 18

Expert Comment

by:wilq32
ID: 22826785
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
 
LVL 18

Expert Comment

by:wilq32
ID: 22826813
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
 
LVL 18

Expert Comment

by:wilq32
ID: 22826823
"(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
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22830153
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:xamian
ID: 22830738
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
 
LVL 18

Expert Comment

by:wilq32
ID: 22830869

<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
 
LVL 18

Expert Comment

by:wilq32
ID: 22830870
try this one ;)
0
 
LVL 1

Author Comment

by:xamian
ID: 22831725
Sorry wilq32 it didn't work, at least in IE 7. Keep getting object expected error.
0
 
LVL 18

Accepted Solution

by:
wilq32 earned 200 total points
ID: 22831780
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
 
LVL 1

Author Closing Comment

by:xamian
ID: 31510865
Awesome solution, I never would have thought about that, thanks a lot.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now