I have a div inside form something like
<form>
<div>
showing some information here
</div>
<div id="idshow" style="display:none">
information here
</div>
</form>
i am population information inside div(idshow) on some button click event. what i want whenever i ill click outside div(idshow), it should be hide. just like i click on menu then menu is display and when i click outside menu it goes hide. I need everything using jquery
Using jQuery The most common approach to hide an element in jQuery is to use the . hide() method. It works by setting the display CSS property to none .
The document. getElementById will select the div with given id. The style. display = "none" will make it disappear when clicked on div.
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.
JavaScript – Hide Div To hide a div using JavaScript, get reference to the div element, and assign value of "none" to the element. style. display property.
$(document).click(function(event) {
var target = $( event.target );
// Check to see if the target is the div.
if (!target.is( "div#idshow" )) {
$("div#idshow").hide();
// Prevent default event -- may not need this, try to see
return( false );
}
});
You can do what you want like this:
$(document).click(function() {
$("#idshow").hide();
});
$("#idshow").click(function(e) {
e.stopPropagation();
});
What happens here is when you click, the click
event bubbles up all the way to document
, if it gets there we hide the <div>
. When you click inside that <div>
though, you can stop the bubble from getting up to document
by using event.stopPropagation()
...so the .hide()
doesn't fire, short and simple :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With