Show or Hide a div with JavaScript

Reply

Join Date: Feb 2008
Posts: 7
Reputation: tarsem is an unknown quantity at this point 
Solved Threads: 1
tarsem's Avatar
tarsem tarsem is offline Offline
Newbie Poster

Show or Hide a div with JavaScript

 
0
  #1
Mar 31st, 2008
HTML and CSS Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>Show hide content with sliding effect</title>
  5. <meta name="language" content="English">
  6. <meta name="robots" content="follow">
  7. <meta name="googlebot" content="index, follow">
  8. <style type="text/css">
  9.  
  10. body{
  11. font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; /* Font to use */
  12. margin:0px;
  13.  
  14. }
  15.  
  16. .ad{
  17. position:absolute;
  18. top:10px;
  19. right:10px;
  20. }
  21.  
  22. .dhtmlgoodies_question{ /* Styling question */
  23. /* Start layout CSS */
  24. color:#FFF;
  25. font-size:0.9em;
  26. background-color:#317082;
  27. width:430px;
  28. margin-bottom:2px;
  29. margin-top:2px;
  30. padding-left:2px;
  31. background-image:url('images/bg_answer.gif');
  32. background-repeat:no-repeat;
  33. background-position:top right;
  34. height:20px;
  35.  
  36. /* End layout CSS */
  37.  
  38. overflow:hidden;
  39. cursor:pointer;
  40. }
  41. .dhtmlgoodies_answer{ /* Parent box of slide down content */
  42. /* Start layout CSS */
  43. border:1px solid #317082;
  44. background-color:#E2EBED;
  45. width:400px;
  46.  
  47. /* End layout CSS */
  48.  
  49. visibility:hidden;
  50. height:0px;
  51. overflow:hidden;
  52. position:relative;
  53.  
  54. }
  55. .dhtmlgoodies_answer_content{ /* Content that is slided down */
  56. padding:1px;
  57. font-size:0.9em;
  58. position:relative;
  59. }
  60.  
  61. </style>
  62. <script type="text/javascript">
  63.  
  64. var dhtmlgoodies_slideSpeed = 10; // Higher value = faster
  65. var dhtmlgoodies_timer = 10; // Lower value = faster
  66.  
  67. var objectIdToSlideDown = false;
  68. var dhtmlgoodies_activeId = false;
  69. var dhtmlgoodies_slideInProgress = false;
  70. function showHideContent(e,inputId)
  71. {
  72. if(dhtmlgoodies_slideInProgress)return;
  73. dhtmlgoodies_slideInProgress = true;
  74. if(!inputId)inputId = this.id;
  75. inputId = inputId + '';
  76. var numericId = inputId.replace(/[^0-9]/g,'');
  77. var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);
  78.  
  79. objectIdToSlideDown = false;
  80.  
  81. if(!answerDiv.style.display || answerDiv.style.display=='none'){
  82. if(dhtmlgoodies_activeId && dhtmlgoodies_activeId!=numericId){
  83. objectIdToSlideDown = numericId;
  84. slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
  85. }else{
  86.  
  87. answerDiv.style.display='block';
  88. answerDiv.style.visibility = 'visible';
  89.  
  90. slideContent(numericId,dhtmlgoodies_slideSpeed);
  91. }
  92. }else{
  93. slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
  94. dhtmlgoodies_activeId = false;
  95. }
  96. }
  97.  
  98. function slideContent(inputId,direction)
  99. {
  100.  
  101. var obj =document.getElementById('dhtmlgoodies_a' + inputId);
  102. var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
  103. height = obj.clientHeight;
  104. if(height==0)height = obj.offsetHeight;
  105. height = height + direction;
  106. rerunFunction = true;
  107. if(height>contentObj.offsetHeight){
  108. height = contentObj.offsetHeight;
  109. rerunFunction = false;
  110. }
  111. if(height<=1){
  112. height = 1;
  113. rerunFunction = false;
  114. }
  115.  
  116. obj.style.height = height + 'px';
  117. var topPos = height - contentObj.offsetHeight;
  118. if(topPos>0)topPos=0;
  119. contentObj.style.top = topPos + 'px';
  120. if(rerunFunction){
  121. setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
  122. }else{
  123. if(height<=1){
  124. obj.style.display='none';
  125. if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
  126. document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
  127. document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
  128. slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);
  129. }else{
  130. dhtmlgoodies_slideInProgress = false;
  131. }
  132. }else{
  133. dhtmlgoodies_activeId = inputId;
  134. dhtmlgoodies_slideInProgress = false;
  135. }
  136. }
  137. }
  138.  
  139.  
  140.  
  141. function initShowHideDivs()
  142. {
  143. var divs = document.getElementsByTagName('DIV');
  144. var divCounter = 1;
  145. for(var no=0;no<divs.length;no++){
  146. if(divs[no].className=='dhtmlgoodies_question'){
  147. divs[no].onclick = showHideContent;
  148. divs[no].id = 'dhtmlgoodies_q'+divCounter;
  149. var answer = divs[no].nextSibling;
  150. while(answer && answer.tagName!='DIV'){
  151. answer = answer.nextSibling;
  152. }
  153. answer.id = 'dhtmlgoodies_a'+divCounter;
  154. contentDiv = answer.getElementsByTagName('DIV')[0];
  155. contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';
  156. contentDiv.className='dhtmlgoodies_answer_content';
  157. contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
  158. answer.style.display='none';
  159. answer.style.height='1px';
  160. divCounter++;
  161. }
  162. }
  163. }
  164. </script>
  165.  
  166.  
  167. </head>
  168. <body>
  169. <br>
  170.  
  171. <img src="/images/heading3.gif">
  172. <div class="dhtmlgoodies_question">Q: What are the advantages of table less design?</div>
  173. <div class="dhtmlgoodies_answer">
  174. <div>
  175. Ohh! There are so many:
  176. <ul>
  177. <li>Faster loading pages</li>
  178. <li>Smoother loading pages</li>
  179. <li>Saved bandwidth</li>
  180.  
  181. <li>Separate layout from content</li>
  182. <li>Easy to change layout</li>
  183. <li>Increased accessibility</li>
  184. <li>Different styling for different media(print, screen, pda)</li>
  185. </ul>
  186. </div>
  187. </div>
  188.  
  189. <div class="dhtmlgoodies_question">Q: What is the difference between &lt;DIV> and &lt;SPAN> ?</div>
  190. <div class="dhtmlgoodies_answer">
  191. <div>
  192. DIV is a block element while SPAN is an inline element. What's common to them both is that none of them have any default styling.
  193. </div>
  194. </div>
  195. <div class="dhtmlgoodies_question">Q: What kind of site is dhtmlgoodies.com ?</div>
  196. <div class="dhtmlgoodies_answer">
  197. <div>
  198. dhtmlgoodies.com is a private site developed and maintained by Alf Magne Kalleland. Here, you will find a lot of
  199. DHTML scripts you can use freely to enhance your website.
  200. </div>
  201.  
  202. </div>
  203. <script type="text/javascript">
  204. initShowHideDivs();
  205. showHideContent(false,1); // Automatically expand first item
  206. </script>
  207. <!-- Kontera ContentLink -->
  208. <script type="text/javascript">
  209. var dc_UnitID = 14;
  210. var dc_PublisherID = 33781;
  211. var dc_AdLinkColor = 'blue';
  212. var dc_isBoldActive= 'no';
  213. var dc_adprod='ADL';
  214. </script>
  215. <script type="text/javascript"
  216. src="http://kona.kontera.com/javascript/lib/KonaLibInline.js"></script>
  217. <!-- Kontera ContentLink -->
  218. </body>
  219. </html>
Last edited by tarsem; Mar 31st, 2008 at 8:44 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 484
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Show or Hide a div with JavaScript

 
0
  #2
Apr 1st, 2008
Ehmm, is this a question or sugested solution for people that may want to use it in their projects?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC