javascript - How to trigger the toggle on one click instead of two -


i using simple toggle this:

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

and html this:

 <div id="income">  <h5 onclick="toggle_visibility('incometoggle');">income</h5>  <div id="incometoggle">     <h6>income total</h6>   </div>    </div>   

at moment, need click twice on heading div close. how can make close 1 click?

http://jsfiddle.net/4w3ynj40/

first check condition display none

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

jquery

  $("h5").click(function() {     $("#incometoggle").toggle();    }); 

demo


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -