tags:

views:

263

answers:

3

I want to show() "<div id="test">write text here</div" on click and not on page load.

<script type="text/javascript">
$(document).ready(function(){

    $("#click_test").stop(function(event){
        event.preventDefault();
        $("#test").slideup();
    });

    $("#click_test").click(function(event){
        event.preventDefault();
        $("#test").toggle();
    });

});
</script>

</head>

<body>

<a href="#" id="click_test">Click Here</a>
<div id="test">write text here</div>

Thanks

A: 

add $("#test").hide(); in your javascript:

  <script type="text/javascript">
    $(document).ready(function(){
        $("#test").hide();

        $("#click_test").stop(function(event){
            event.preventDefault();
            $("#test").slideup();
        });

        $("#click_test").click(function(event){
            event.preventDefault();
            $("#test").toggle();
        });

    });
  </script>

Alternatively in your html:

<div id="test" style="display:none">write text here</div>

gives the same result

Thomas Stock
thanks........................ had to get to 15 chars to say a thanks
+1  A: 

The best solution (imho) is to hide it to start with rather than doing that with Javascript.

As for the link, just return false to stop the browser going to the href. If you want the link to toggle the div then replace show() with toggle(). You can of course use any of the other jQuery effects (eg slide or fade).

<style type="text/css">
#test { display: none; }
</style>
<script type="text/javascript">
$(function() {
  $("#click_text").click(function() {
    $("#test").show();
    return false;
  });
});
</script>
<a href="#" id="click_test">Click Here</a>
<div id="test">write text here</div>
cletus
does not work ,
Um... what doesn't work?
cletus
when u click on the #test nothing happens
A: 

You should use :

<div id="test" style="display:none">write text here</div>

Be careful: In this case, the div will remain hidden for browser with javascript deactivated.
If you want those browser to see the div, use instead:

$(function() {
    $("#test").hide();
});
Nico