Alice Alice - 3 months ago 13
jQuery Question

How to write jquery If else statement?

This code shows frm01:

$(document).ready(function() {
$("#reg").click(function () {
$("#frm01").show("slide", { direction: "down" }, 1000);
});
});


But I want to hide frm01 if it is allready visible, and vice versa.
How could I do this, please ?

Answer

Try jQuery's toggle() method:

$(function() {
    $("#reg").click(function () {
        $("#frm01").toggle(1000);
    });
});

You don't need jQuery to use if-else statements; Javascript implements those natively...

$(function() {
    $("#reg").click(function () {
        if ($("#frm01").is(":visible"))
            $("#frm01").slideUp(1000);
        else
            $("#frm01").slideDown(1000);
    });
});
Comments