Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide div when page load

I have jquery issue, Kindly see my jquery code:

$(document).ready(function(){

    $(".toggle_container").show();

    $("h2.trigger").toggle(function(){
            $(this).addClass("active");
            }, function () {
            $(this).removeClass("active");
    });

    $("h2.trigger").click(function(){
            $(this).next(".toggle_container").slideToggle("slow,");
    });

});

My .toggle_container is shown always its good, But I want to close it first time, when page load. Can you please provide me any solution? I dont want to use $(".toggle_container").hide(); function for this problem because when i click on href then .toggle_container should not hide, It should open.

Regards.

like image 358
Riz Avatar asked May 23 '11 07:05

Riz


3 Answers

You can just add attribute

style="display:none"

to your element .toggle_container.

Then on first call of $(document).ready it will be shown.

The full test example:

<html>
<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("h2.trigger").click(function(){
        $(this).next(".toggle_container").slideToggle("slow,");
      });
    });
  </script>
</head>
<body>
  <h2 class="trigger">Toggle</h2>
  <div class="toggle_container" style="display:none">Container</div>
</body>
</html>

Note: there`s no $(".toggle_container").show(); on $(document).ready

like image 92
Hck Avatar answered Oct 04 '22 20:10

Hck


remove the

$(".toggle_container").show();

from your $(document).ready function.

in html part add style="display:none" for the toggle_container div.

check the @HCK s reply. he is clearly mentioned it..

like image 44
Rifky Avatar answered Oct 04 '22 22:10

Rifky


Once the document gets loaded, a alert box will be prompted "page loaded".

$(document).ready(function(){    
    alert('page loaded');  // alert to confirm the page is loaded    
    $('.divClassName').hide(); //enter the class or id of the particular html element which you wish to hide. 
});
like image 45
Vinit Kadkol Avatar answered Oct 04 '22 20:10

Vinit Kadkol