Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a div on document ready, and then call it dynamically from Javascript?

I'm using this code to hide a div container where I'm placing text dynamically.

$(document).ready(function(){
     $(".slidingDiv").hide(); 
     $(".show_hide").show();

     $('.show_hide').click(function(){
         $(".slidingDiv").slideToggle();
     });
 });

The problem I have is that I want to trigger the show div from a javascript function instead of a predefined click event. I've found this example which mateches the function I want, but I'm not sure how to trigger it from javascript instead of a click function.

JSFiddle Here

like image 572
blarg Avatar asked Mar 13 '13 10:03

blarg


People also ask

How do I hide a div in document ready?

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 . Now the document is rendered as though the element did not exist.

Can you hide a div in Javascript?

To show/hide a div element by id: Access the style. display property on the div element. If the value of the display property is set to none , set it to block .

How do I hide a specific div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.

How do you hide a div after clicking it?

To display or hide a <div> by a <button> click, you can add the onclick event listener to the <button> element. The onclick listener for the button will have a function that will change the display attribute of the <div> from the default value (which is block ) to none .


1 Answers

just hide the div using css

display:none;

.slidingDiv{
 display:none;
}

and show it when ever you want using

.show()

$(".slidingDiv").show();

edit:

after you question edit, you can always trigger the click event programatically like

function yourFunction(){
  $(".show_hide").click();
} 
like image 150
Dakait Avatar answered Oct 01 '22 12:10

Dakait