Solved

show one div hide other div

Posted on 2008-06-17
4
1,673 Views
Last Modified: 2012-06-27
Yellow!

I have problem!

I need action that one of div tags are visible.. if click on link then first div hides and other show...
<script>
function setVisibility(id) {
      var dObj = document.getElementById (id);
      if (!dObj) { return; }
     var d = (dObj.style.display.match('block')) ? 'none': 'block';
     dObj.style.display =d;
     return false;
}
</script><table border=1><tr><td height="100" >
<a href="javascript:void(0);" onclick="return setVisibility('div1');setVisibility('div2');">Show Hide</a>
<div  id="div1" style="display:none;">DIV 1<br /><br /><br /><br /><br />DIV<br /><br /><br /><br /><br /><br /><br /><br />DIV</div>
<div  id="div2" style="display:block;">DIV 1<br /><br /><br /><br /><br />DIV<br /><br /><br /><br /><br /><br /><br /><br />DIV</div>
 
</td></tr></table>

Open in new window

0
Comment
Question by:lamzaks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:TheVanter
ID: 21807850
I don't think you can have 2 functions happen onclick, you have to combine them into one.
There's other ways to write a program to do both, but here's one example:
function setVisibility(id1, id2) {
     var dObj1 = document.getElementById (id1);
     var dObj2 = document.getElementById (id2);
      if (!dObj1 || !dObj2) { return; }
     var d1 = (dObj1.style.display.match('block')) ? 'none': 'block';
     var d2 = (dObj2.style.display.match('block')) ? 'none': 'block';
     dObj1.style.display =d1;
     dObj2.style.display =d2;
     return false;
}
 
. . .
 
<a href="javascript:void(0);" onclick="return setVisibility('div1', 'div2');">Show Hide</a>

Open in new window

0
 
LVL 1

Author Comment

by:lamzaks
ID: 21807902
sorry.. do not working.. :(

and in some pages divs will be more than 10

I div1 and div2 use like variable!
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21811357
Try this simple script,
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Untitled Document</title>
</head>
 
<body>
 
  
<script type="text/javascript" language="javascript">
 
 
function ShowDiv2()
{
        document.getElementById("div2").style.display = "";
        document.getElementById("div1").style.display = "none";
}
 
function ShowDiv1()
{
        document.getElementById("div1").style.display = "";
        document.getElementById("div2").style.display = "none";
}
 
</script> 
 
 
 
 
</div>
 
<form id="form1" name="form1" method="post" action="">
  <table width="95%" border="0" cellspacing="3" cellpadding="3">
    <tr>
      <td width="32%">&nbsp;</td>
      <td width="68%"><label>
        <input name="radiobutton" type="radio" value="radiobutton"  onclick="javascript:ShowDiv2();"/>
      Check Option1
      <input name="radiobutton" type="radio" value="radiobutton" onclick="javascript:ShowDiv1();"/>
      Check Option2 </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>
	  <div id="div1" style="display:none" >
        <table width="49%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="20%">Add1</td>
            <td width="80%">Sample Address </td>
          </tr>
          <tr>
            <td>Add2</td>
            <td>Sample Address </td>
          </tr>
        </table>
		</div>
 
        <div id="div2" style="display:none" >
		<table width="50%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="20%">Add2</td>
            <td width="80%">Adsdress box </td>
          </tr>
          <tr>
            <td>Add2</td>
            <td>Address box </td>
          </tr>
        </table>
		</div>      </td>
    </tr>
  </table>
</form>
</body>
</html>

Open in new window

0
 
LVL 2

Accepted Solution

by:
TheVanter earned 50 total points
ID: 21813562
I think the point was to have just one button control them both (or all). I don't know why you had a problem getting what I wrote earlier to work. Maybe you inserted it into your own code wrong?
Either way, if you have a variable number of divs you want to toggle (more than 10), editing the program each time is not going to work.
One way would be to recurse through divs, as long as you've got a consistent naming scheme. You might want to add something that checks to make sure that each one exists, but this works fine. Just change the number in the toggleVisibility() call to the number of divs you have.
<head>
<script type="text/javascript" language="javascript">
function toggleVisibility(id) {
for (var i=1;i<=id;i++)
{
var thisdiv = 'div' + i;
if (document.getElementById(thisdiv).style.display == 'block')
	{
	document.getElementById(thisdiv).style.display = 'none';
	}
else
	{
	document.getElementById(thisdiv).style.display = 'block';
	}
}
     return false;
}
</script>
</head>
<body>
<table border=1><tr><td height="100" >
<a href="javascript:void(0);" onclick="return toggleVisibility(4);">Show Hide</a>
<div  id="div1" style="display:none;">DIV 1<br /><br /><br />DIV<br /><br /></div>
<div  id="div2" style="display:block;">DIV 2<br /><br /><br />DIV<br /><br /></div>
<div  id="div3" style="display:none;">DIV 3<br /><br /><br />DIV<br /><br /></div>
<div  id="div4" style="display:block;">DIV 4<br /><br /><br />DIV<br /><br /></div>
</td></tr></table></body>

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

636 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