show one div hide other div

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

LVL 1
lamzaksAsked:
Who is Participating?
 
TheVanterConnect With a Mentor Commented:
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
 
TheVanterCommented:
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
 
lamzaksAuthor Commented:
sorry.. do not working.. :(

and in some pages divs will be more than 10

I div1 and div2 use like variable!
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
All Courses

From novice to tech pro — start learning today.