Jump to content

unblock and block a div


arbitter

Recommended Posts

Hi there,

 

My javascript knowledge is very tiny. I can show a div using the onClick; example:

<a href='#' onClick=\"document.getElementById('theDiv').style.display='block';\">Show div.</a>
<div id='theDiv' style='display:none;'>I'll be shown if you click it </div>

 

Now what I want to do is be able to click the same link again, and hide the div again. So actually a toggle between shown and un-shown.

 

Any help will be highly appreciated!

Link to comment
Share on other sites

there are toggle functions built into framework languages like jquery that might, but what I would do in your situation is set a js variable that tells you the current state of the div. then when the link is clicked, do the opposite and update the state.

 

var display_div = false;

function display_or_hide(){

  if(!display_div){

  document.getElementById('theDiv').style.display='none';

  display_div = true;

  }else{

  document.getElementById('theDiv').style.display='block';

  display_div = false;

}

}

 

then set your html like:

onClick="display_or_hide()">Show div.</a>

 

Link to comment
Share on other sites

<html>
<head>
<script type="text/javascript">

function showHideDiv(linkObj, divID)
{
    var showDiv = (linkObj.innerHTML!='Hide div.');
    document.getElementById(divID).style.display = (showDiv) ? 'block' : 'none';
    linkObj.innerHTML = (showDiv) ? 'Hide div.' : 'Show div.';
    return;
}
</script>
</head>
<body>

<a href="#" onClick="showHideDiv(this, 'theDiv');">Show div.</a>
<div id='theDiv' style='display:none;'>I'll be shown if you click it </div>

</body>
</html>

Link to comment
Share on other sites

That code seems to work nicely, thanks a lot! :)

 

But what if I do not want to change what the link says?

 

edit: with your code, micah1701, I can hide and unhide without the link value to change. Problem is though, that when it' clicked for the first time, you have to click twice. Then the div gets shown. And from then on, you only have to click once to hide or show it...

 

edit2: changed the block and the none from place in the javascript, works nicely now!

 

<script type='text/javascript'>
var display_div = false;
function display_or_hide(){
  if(!display_div){
   document.getElementById('loginForm').style.display='block';
   display_div = true;
  }else{
   document.getElementById('loginForm').style.display='none';
   display_div = false;
}
}
</script>
<?php
echo "<div onClick=\"display_or_hide()\">Click on me!</div>";

echo "<div id='loginForm' style='display:none;'>I play hide 'n' seek!</div>";

?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.