56545

Toggling visibility of divs using javascript

Question:

I have the following code, for enabling a div from toggling between visible and hidden:

function toggle_visibility(id,$this) { var e = document.getElementById(id); if(e.style.display == 'block') { e.style.display = 'none'; } else { e.style.display = 'block'; } }

Basically when I click a link that has onclick="toggle_visibility('4s'); within it then the specified div is shown and then when you click again it is hidden.

My problem is when the same code is use for multiple links, and you toggle the visibility of one and then the other, the previous one is still shown. How would I go about only enabling one div to be shown when toggled and then if another is toggled the other is hidden?

Answer1:

<a href="http://jsfiddle.net/LdrdG/" rel="nofollow">Here</a> is a generic example using jquery.

<strong>Markup</strong>

<section> <div id="1">one</div> <div id="2">two</div> <div id="3">three</div> <div id="4">four</div> </section> <a href="#1">one</a> <a href="#2">two</a> <a href="#3">three</a> <a href="#4">four</a>

<strong>JS</strong>

var divs = $('section div'), links = $('a'); links.click(function(){ $(this.hash).toggle().siblings().hide(); return false; });

Answer2:

Keep a global variable for the div which is currently visible and make it invisible when you make any div visible.

var previousVisibleElement; function toggle_visibility(id,$this) { var e = document.getElementById(id); if(e.style.display == 'block') { e.style.display = 'none'; } else { if(previousVisibleElement !=null) previousVisibleElement.style.display='none'; e.style.display = 'block'; previousVisibleElement=e; } }

Answer3:

if you provide the current script in a jsfiddle people would be able to answer better.

here is a small sample i did using jquery. <a href="http://jsfiddle.net/pramodpv/zzyRx/" rel="nofollow">hide using jquery toggle </a>

Recommend

  • Disabling apps using the chrome.management API
  • Google Geocoder Asynchronous Issue
  • jQuery SlideToggle Google Maps Issue
  • How should feature toggles be set in tests run in continuous integration?
  • android resolution need help
  • Switch case in Data Binding
  • Ignoring a field without modifying the POJO class with Jackson
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Send data from edittext to listview
  • Responsive left sidebar open close
  • How to create two column output from a single column
  • Prevent page break in text block with iText, XMLWorker
  • Most efficient way to move table rows from one table to another
  • Jquery Mobile pageLoading() Method how does it work?
  • C++ friend class std::vector
  • How can I get the choice “H2” back in the H2 consol?
  • SyntaxError: (irb):26: both block arg and actual block given
  • Approximate Order-Preserving Huffman Code
  • Simulate click Geckofx vb,net
  • Webgrid not refreshing after delete MVC
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Jquery UI tool tip close icon
  • How to delay loading a property with linq to sql external mapping?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Updated Ionic CLI but shows previous version (Windows)
  • ORA-29908: missing primary invocation for ancillary operator
  • jquery mobile loadPage not working
  • Traverse Array and Display in markup
  • Data Validation Drop Down Box Arrow Disappearing
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do you join a server to an Active Directory (domain)?
  • Qt: Run a script BEFORE make
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?