Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Toggle two function not work in 1.10.1

Tags:

jquery

toggle

Jquery Toggle two function not work in jquery 1.10.1 but work's on Jquery 1.8.3

HTML

<p>For example, consider the HTML:</p>
                <div id="target">
                  Click here
                </div>

Jquery

$('#target').toggle(function() {
    alert('First handler for .toggle() called.');
}, function() {
    alert('Second handler for .toggle() called.');
});

and expmple is here

like image 668
Malde Chavda Avatar asked Aug 13 '13 09:08

Malde Chavda


4 Answers

Dude it doesn't works in jQuery 1.10.1

Still there is another way to do it...

$(function () {
    function first() {
       //Code for first time click goes here
        $(this).one("click", second);
    }
    function second() {
        //Code for second time click goes here
        $(this).one("click", first);
    }
    $("#target").one("click", first);
});
like image 140
Tushar Pandya Avatar answered Nov 18 '22 17:11

Tushar Pandya


jQuery.toggle has been deprecated since version 1.8 and removed sometime later. You can write your own implementation instead:

function handler1() {
    alert("first handler for .toggle() called.");
    $(this).one("click", handler2);
}
function handler2() {
    alert("second handler for .toggle() called.");
    $(this).one("click", handler1);
}
$("#target").one("click", handler1);

Demo

like image 24
Salman A Avatar answered Nov 18 '22 19:11

Salman A


DEMO

DEMO jQuery 2.x (edge)

use this clickToggle plugin as toggle function is removed from version 1.9

(function ($) {
    $.fn.clickToggle = function (func1, func2) {
        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function () {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
}(jQuery));
$('#target').clickToggle(function () {
    alert('First handler for .toggle() called.');
}, function () {
    alert('Second handler for .toggle() called.');
});
like image 6
Tushar Gupta - curioustushar Avatar answered Nov 18 '22 17:11

Tushar Gupta - curioustushar


This format was removed in version 1.9, so you cannot use it - if you want this logic can be manually implemented or you can make use of the migration plugin

to include the migration plugin, after the inclusion of jQuery library add

<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>
like image 5
Arun P Johny Avatar answered Nov 18 '22 18:11

Arun P Johny