Reputation: 317
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").toggle(function(){
$("p").hide();},
function(){
$("p").show();}
});
});
</script>
</head>
<body>
<p>Click me to toggle between different background colors.</p>
<button>click me</button>
</body>
</html>
i am trying to toggle between hide and show on button click...but it's not working a little help will be appreciable
Upvotes: 1
Views: 222
Reputation: 2795
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<p>Click me to toggle between different background colors.</p>
<button>click me</button>
</body>
</html>
Upvotes: 0
Reputation: 10374
You can write simply:
$("button").toggle(function(){
$("p").hide();},
function(){
$("p").show();}
});
Upvotes: 0
Reputation: 14250
Try this http://jsfiddle.net/h45En/
$('a').bind('click', function() {
$('p').toggle();
});
toggle will hide the $('p') by default.
Upvotes: 0
Reputation: 23542
To toggle the background color use toggleClass()
and CSS
$(document).ready(function() {
$("button").click(function() {
$("p").toggleClass('red');
});
});
See the manual http://api.jquery.com/toggle/
Upvotes: 0