I have simple hide and show function but it is not working. any help would be appreciated
<head>
<script type="text/javascript">
$(function(){
$('a').click(function(){
var visible= false;
if(visible){
$('div').hide('slow')
}
else {
$('div').show('slow')
}
visible=!visible;
})
})
</script>
</head>
<body>
<div style="background:#F00; width:500px; height:50px"></div>
<a href="#">hide</a>
<div class="append"></div>
</body>
Declare and initialise
visibleoutside the event handler:Otherwise you are initialising
visiblewithfalseevery time the event handler is executed, soif(visible)will never be true.However, you can also use
.toggle[docs] to avoid having an additional status variable: