I am writing something similar to the following code a lot. It basically switches the item based on some condition.
In the following prepared example, the condition is: "If the agree check box is agree , and the name field is not empty."
$("button").click(function() { if ($("#agree").is(":checked") && $("#name").val() != "" ) { $("#mydiv").show(); } else { $("#mydiv").hide(); } });
I want some jQuery function to work like this.
$("button").click(function() { var condition = $("#agree").is(":checked") && $("#name").val() != "" ); $("#mydiv").toggle(condition); });
Is there something similar? Or are there other ways than the first example to do this less than if-else-ish ?
javascript jquery toggle
jessegavin Mar 05 '10 at 17:30 2010-03-05 17:30
source share