Click here to Skip to main content
15,886,541 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
See more:
i have

<body önclick=javascript:hide();>

div onclick="javascript:show">




when i click on div it shows the div which is going 2 open
but @same time doc click event
gets fired and it hide the div

please suggest
Posted

One method is using the CSS visibility style 'collapse' or 'hidden', depending on the desired look:
JavaScript
div = document.getElementById('myDivId')
div.style.visibility="hidden" /* hide */
/*...*/
div.style.visibility="collapse" /* collapse */
/*...*/
div.style.visibility="visible" /* show again */

Another variant of the above:
JavaScript
function hide(object) { object.style.visibility = "hidden"; }
function show(object) { object.style.visibility = null; }

See visibility — CSS | MDN[^].

Another way is using the CSS display style 'none' or 'block':

JavaScript
div.style.display="none" /* hide */
/*...*/
div.style.display="block" /* show again */
See display — CSS | MDN[^].

In addition to all that, it's sometimes a good idea not to change styles directly, but predefine one or more CSS classes and remove or add classes from/to the HTML element, to achieve desired effect at once, or to combine several effects at once.

One approach is to modify class list: Element.classList — Web APIs | MDN[^].

Another approach is to modify the value if the class attribute, which represents the name of some CSS class: Element.setAttribute() — Web APIs | MDN[^].


—SA
 
Share this answer
 
v7
Use this jQuery script.

XML
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(#divID).click(function(){
    $(this).hide();
  });
  $(#divID).click(function(){
    $(this).show();
  });
});
</script>


Regards,
Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900