Prakash Chandra
Prakash Chandra

Reputation: 317

toggle is not working

<!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

Answers (5)

Sunil Kumar B M
Sunil Kumar B M

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

Hadas
Hadas

Reputation: 10374

You can write simply:

$("button").toggle(function(){
    $("p").hide();},
    function(){
    $("p").show();}
});

Upvotes: 0

czerasz
czerasz

Reputation: 14250

Try this http://jsfiddle.net/h45En/

$('a').bind('click', function() {
    $('p').toggle();
});

toggle will hide the $('p') by default.

Upvotes: 0

PiTheNumber
PiTheNumber

Reputation: 23542

To toggle the background color use toggleClass() and CSS

$(document).ready(function() {
    $("button").click(function() {
        $("p").toggleClass('red');
    });
});

http://jsfiddle.net/M7dpB/1/

See the manual http://api.jquery.com/toggle/

Upvotes: 0

alex
alex

Reputation: 490183

You have a SyntaxError. Don't forget to close the parenthesis after the closing brace of toggle().

If you are only toggling the visibility of the element, you should just be able to use toggle() with no arguments.

jsFiddle.

Upvotes: 3

Related Questions