2

I have a chunk of code like this:

$(".ani-search").toggle(
    function(){
        $("#header .logo a").animate({
            marginLeft: "-=30px",
            marginLeft: "-=60px",
            marginLeft: "-=90px"
        });
    },

    function(){
        $("#header .logo a").animate({
            marginLeft: "-=60px",
            marginLeft: "-=30px",
            marginLeft: "0px"
      })
   }
);

When I run the corresponding html page, I do not get any VALID toggle response. What happens is as soon as I open the page the image with class "ani-search" blinks once and vanishes thats it, I do not get to toggle anything? Why is this happening?

Similarly here is another test code"

<p>Hello world</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.0.js"><\/script>')</script>
<script>
    $("p").toggle(
        function(){
            alert("Cliclk 1");
        },
        function(){
            alert("Click 2");
        }
    );    
</script>

same thing happens here, as soon as I load the page in my browser, the text "Hello world" blinks once and I get the alert box with message with "Click 2". Thats it.

Why cant I toggle here?

Swaroop Nagendra
  • 609
  • 2
  • 15
  • 25

2 Answers2

3

Deprecated and removed, create your own toggle functionality :

$(".ani-search").on('click', function() {
    if (!$(this).data('state')) {
        $("#header .logo a").animate({
            marginLeft: "-=30px",
            marginLeft: "-=60px",
            marginLeft: "-=90px"
        });
    }else{
        $("#header .logo a").animate({
            marginLeft: "-=60px",
            marginLeft: "-=30px",
            marginLeft: "0px"
        });
    }
    $(this).data('state', !$(this).data('state'));
});

FIDDLE

note that adding the same CSS property multiple times does not animate it multiple times, only the last one is used.

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

toggle is gone. You might want to use data attribute to do this by yourself :

$("p").on("click", function () {
    if ($(this).data("click")) {
        alert("Click 2");
        $(this).data("click", false);
    } else {
        alert("Cliclk 1");
        $(this).data("click", true);
    }
});
krishwader
  • 11,341
  • 1
  • 34
  • 51