Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write jquery If else statement?

Tags:

jquery

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 ?

like image 457
Alice Avatar asked Jul 15 '12 21:07

Alice


People also ask

How call jQuery function in if condition?

getElementById("title"). value; var flagu2=0; ..... ..... var flagu6=0; if( flagu1==0 && flagu2==0 && flagu3==0 && flagu4==0 && flagu6==0 ) return true; else return false; } function clearBox(type) { // Your implementation here } // Event handler $submitButton. on('click', handleSubmit); });

What is '$' in jQuery?

$ is pretty commonly used as a selector function in JS. In jQuery the $ function does much more than select things though. You can pass it a selector to get a collection of matching elements from the DOM. You can pass it a function to run when the document is ready (similar to body.

What is else if in JavaScript?

In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


1 Answers

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);
    });
});
like image 172
Matt Avatar answered Sep 24 '22 10:09

Matt