jQuery > Events

Toggle event in jQuery

How to attach toggle event to a html element in jQuery?

To attach toggle events that fires in alternate clicks, toggle() method can be used.

        $("#divToggle").toggle(function () {
            $(this).css("background", "red");
function () {
    $(this).css("background", "green");
function () {
    $(this).css("background", "");

In the above code when "divToggle” is clicked for the first time the background color of this element becomes red, the next time it becomes green and when clicked again it will be transparent and again will repeat the same.

Notice that all three effects are written separated by ",".

 Views: 767 | Post Order: 86

Write for us