Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery- Hide div

Tags:

jquery

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

like image 650
Pankaj Avatar asked May 28 '10 10:05

Pankaj


People also ask

Can we hide div in 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 .

How do I hide a div?

The document. getElementById will select the div with given id. The style. display = "none" will make it disappear when clicked on div.

What does hide () do in jQuery?

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.

How can I hide div in JS?

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.


2 Answers

$(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 );
  }
});
like image 115
Tauren Avatar answered Oct 07 '22 18:10

Tauren


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

like image 33
Nick Craver Avatar answered Oct 07 '22 19:10

Nick Craver