css - Toggling visibility of divs using javascript - Stack Overflow

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

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?

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?

Share Improve this question edited Oct 12, 2011 at 16:52 namuol 9,9966 gold badges43 silver badges54 bronze badges asked Oct 12, 2011 at 16:31 Alex SaidaniAlex Saidani 1,3172 gold badges16 silver badges32 bronze badges 2
  • please give us HTML also – Wasim Karani Commented Oct 12, 2011 at 16:34
  • 1 Either store the id of the previous div and hide that when the function is next called, or just blanket hide all divs before showing the current one. – geekchic Commented Oct 12, 2011 at 16:35
Add a ment  | 

3 Answers 3

Reset to default 3

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;

  }
}

Here is a generic example using jquery.

Markup

<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>

JS

var divs = $('section div'),
    links = $('a');

links.click(function(){
    $(this.hash).toggle().siblings().hide();
    return false;
});

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. hide using jquery toggle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745444778a4627996.html

相关推荐

  • css - Toggling visibility of divs using javascript - Stack Overflow

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

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信