Solved

show one div hide other div

Posted on 2008-06-17
4
1,662 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

912 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

16 Experts available now in Live!

Get 1:1 Help Now